nuclear@5: #include "config.h" nuclear@5: #include nuclear@5: #include nuclear@5: #include nuclear@5: #include "gbasys.h" nuclear@5: #include "game.h" nuclear@5: nuclear@5: static int proc_events(SDL_Event *ev); nuclear@5: static void handle_keyboard(int key, int state); nuclear@5: nuclear@5: struct pixel_buffer *back_buffer, *front_buffer; nuclear@5: nuclear@5: static SDL_Surface *surf; nuclear@5: static struct pixel_buffer bbuf; nuclear@5: static unsigned int keystate; nuclear@6: static int sdlscale = 2; nuclear@5: nuclear@5: int main(void) nuclear@5: { nuclear@6: int i, j, k, l; nuclear@6: char *env; nuclear@6: nuclear@6: if((env = getenv("SDLSCALE"))) { nuclear@6: if(!(sdlscale = atoi(env))) { nuclear@6: fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env); nuclear@6: sdlscale = 2; nuclear@6: } nuclear@6: } nuclear@5: nuclear@5: SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE); nuclear@5: nuclear@6: if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, 16, SDL_SWSURFACE))) { nuclear@5: fprintf(stderr, "failed to initialize graphics\n"); nuclear@5: return 1; nuclear@5: } nuclear@5: SDL_WM_SetCaption("trycatch", 0); nuclear@5: nuclear@5: bbuf.x = WIDTH; nuclear@5: bbuf.y = HEIGHT; nuclear@5: bbuf.bpp = 16; nuclear@5: if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) { nuclear@5: fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT); nuclear@5: SDL_Quit(); nuclear@5: return 1; nuclear@5: } nuclear@5: nuclear@5: back_buffer = front_buffer = &bbuf; nuclear@5: nuclear@5: for(;;) { nuclear@5: SDL_Event ev; nuclear@5: uint16_t *dest, *src; nuclear@5: nuclear@5: while(SDL_PollEvent(&ev)) { nuclear@5: if(proc_events(&ev) == -1) { nuclear@5: goto done; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: game_draw(); nuclear@5: nuclear@5: if(SDL_MUSTLOCK(surf)) { nuclear@5: SDL_LockSurface(surf); nuclear@5: } nuclear@5: nuclear@5: src = bbuf.pixels; nuclear@5: dest = surf->pixels; nuclear@5: nuclear@5: for(i=0; ipixels; nuclear@5: nuclear@5: for(i=0; ix * pbuf->y; i++) { nuclear@5: *pixels++ = color; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: int get_key_state(int key) nuclear@5: { nuclear@5: return keystate & key; nuclear@5: } nuclear@5: nuclear@5: unsigned long get_millisec(void) nuclear@5: { nuclear@5: return SDL_GetTicks(); nuclear@5: } nuclear@5: nuclear@5: static int proc_events(SDL_Event *ev) nuclear@5: { nuclear@5: switch(ev->type) { nuclear@5: case SDL_KEYDOWN: nuclear@5: case SDL_KEYUP: nuclear@5: handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN); nuclear@5: break; nuclear@5: nuclear@5: case SDL_QUIT: nuclear@5: return -1; nuclear@5: } nuclear@5: nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: static void handle_keyboard(int key, int state) nuclear@5: { nuclear@5: int gba_key = -1; nuclear@5: nuclear@5: switch(key) { nuclear@5: case SDLK_ESCAPE: nuclear@5: { nuclear@5: SDL_Event ev; nuclear@5: ev.type = SDL_QUIT; nuclear@5: SDL_PushEvent(&ev); nuclear@5: } nuclear@5: break; nuclear@5: nuclear@5: case 'a': nuclear@5: case 'A': nuclear@5: case SDLK_LEFT: nuclear@5: gba_key = KEY_LEFT; nuclear@5: break; nuclear@5: nuclear@5: case 'd': nuclear@5: case 'D': nuclear@5: case SDLK_RIGHT: nuclear@5: gba_key = KEY_RIGHT; nuclear@5: break; nuclear@5: nuclear@5: case 'w': nuclear@5: case 'W': nuclear@5: case SDLK_UP: nuclear@5: gba_key = KEY_UP; nuclear@5: break; nuclear@5: nuclear@5: case 's': nuclear@5: case 'S': nuclear@5: case SDLK_DOWN: nuclear@5: gba_key = KEY_DOWN; nuclear@5: break; nuclear@5: nuclear@5: case '\n': nuclear@5: gba_key = KEY_A; nuclear@5: break; nuclear@5: case '\b': nuclear@5: gba_key = KEY_B; nuclear@5: break; nuclear@5: nuclear@5: case ',': nuclear@5: gba_key = KEY_SELECT; nuclear@5: break; nuclear@5: case '.': nuclear@5: gba_key = KEY_START; nuclear@5: break; nuclear@5: nuclear@5: case '[': nuclear@5: gba_key = KEY_L; nuclear@5: break; nuclear@5: case ']': nuclear@5: gba_key = KEY_R; nuclear@5: break; nuclear@5: nuclear@5: default: nuclear@5: return; nuclear@5: } nuclear@5: nuclear@5: keystate |= gba_key; nuclear@5: game_keyb(gba_key, state); nuclear@5: }