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