PageRenderTime 14ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Show/Stage.cpp

http://github.com/mbebenita/Broadway
C++ | 118 lines | 97 code | 21 blank | 0 comment | 12 complexity | 7b7415b520ae6aee404af349794522a3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include <js/jsapi.h>
  2. #include <SDL/SDL.h>
  3. #include "Stage.h"
  4. #define WIDTH 640
  5. #define HEIGHT 480
  6. #define BPP 4
  7. #define DEPTH 32
  8. Stage::Stage(JSRuntime *rt, JSContext *cx, JSObject *global) : rt(rt), cx(cx), global(global) {
  9. Initialize();
  10. }
  11. int
  12. Stage::Play() {
  13. SDL_Event event;
  14. int keypress = 0;
  15. int h = 0;
  16. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  17. return 1;
  18. }
  19. if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE))) {
  20. SDL_Quit();
  21. return 1;
  22. }
  23. screenBufferSize = HEIGHT * screen->pitch;
  24. screenBuffer = (char *)JS_malloc(cx, screenBufferSize + sizeof(uint64));
  25. ((uint64 *)screenBuffer)[0] = screenBufferSize;
  26. printf("Allocated: %d %d \n", screenBufferSize, screenBufferSize + sizeof(uint64));
  27. screenArrayBuffer = JS_CreateArrayBuffer(cx, screenBuffer, screenBufferSize);
  28. while (!keypress) {
  29. DrawScreen(h++);
  30. while (SDL_PollEvent(&event)) {
  31. switch (event.type) {
  32. case SDL_QUIT:
  33. keypress = 1;
  34. break;
  35. case SDL_KEYDOWN:
  36. keypress = 1;
  37. break;
  38. }
  39. }
  40. }
  41. SDL_Quit();
  42. return 0;
  43. }
  44. JSBool JS_SetPixel(JSContext *cx, uintN argc, jsval *vp) {
  45. uint32 x, y, r, g, b;
  46. if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "uuuuu", &x, &y, &r, &g, &b))
  47. return JS_FALSE;
  48. JS_SET_RVAL(cx, vp, JSVAL_VOID);
  49. return JS_TRUE;
  50. }
  51. static JSFunctionSpec stageFunctions[] = {
  52. JS_FS("setPixel", JS_SetPixel, 5, 0),
  53. JS_FS_END
  54. };
  55. int
  56. Stage::Initialize() {
  57. if (!JS_DefineFunctions(cx, global, stageFunctions)) {
  58. return JS_FALSE;
  59. }
  60. return JS_TRUE;
  61. }
  62. void
  63. Stage::Blit(char *dst, const char *src, size_t size) {
  64. for (int y = 0; y < size / screen->pitch; y++) {
  65. int start = y * screen->pitch;
  66. for (int x = 0; x < screen->pitch / BPP; x++) {
  67. int offset = start + x * sizeof(Uint32);
  68. Uint32 srcPixel = *(Uint32 *)(src + offset);
  69. Uint8 r = srcPixel >> 24 & 0xFF;
  70. Uint8 g = srcPixel >> 16 & 0xFF;
  71. Uint8 b = srcPixel >> 8 & 0xFF;
  72. Uint32 *dstPixelAddress = (Uint32 *)(dst + offset);
  73. *dstPixelAddress = SDL_MapRGB(screen->format, r, g, b);
  74. }
  75. }
  76. }
  77. int
  78. Stage::DrawScreen(int h) {
  79. jsval rval;
  80. int x, y, ytimesw;
  81. if (SDL_MUSTLOCK(screen)) {
  82. if (SDL_LockSurface(screen) < 0) {
  83. return 0;
  84. }
  85. }
  86. jsval argv[5];
  87. argv[0] = OBJECT_TO_JSVAL(screenArrayBuffer);
  88. argv[1] = INT_TO_JSVAL(WIDTH);
  89. argv[2] = INT_TO_JSVAL(HEIGHT);
  90. argv[3] = INT_TO_JSVAL(screen->pitch);
  91. argv[4] = INT_TO_JSVAL(BPP);
  92. JSBool ok = JS_CallFunctionName(cx, global, "paint", 5, argv, &rval);
  93. Blit((char *)screen->pixels, screenBuffer, screenBufferSize);
  94. if (SDL_MUSTLOCK(screen))
  95. SDL_UnlockSurface(screen);
  96. SDL_Flip(screen);
  97. }