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@9: #ifdef PALMODE nuclear@9: #define BPP 8 nuclear@9: #define PIXSZ 1 nuclear@9: #else nuclear@9: #define BPP 16 nuclear@9: #define PIXSZ 2 nuclear@9: #endif nuclear@9: 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@9: if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, BPP, 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@9: bbuf.bpp = BPP; nuclear@9: if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * PIXSZ))) { 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@8: if(game_init() == -1) { nuclear@8: SDL_Quit(); nuclear@8: return 1; nuclear@8: } nuclear@8: nuclear@5: for(;;) { nuclear@5: SDL_Event ev; nuclear@9: #ifdef PALMODE nuclear@9: uint8_t *dest, *src; nuclear@9: #else nuclear@5: uint16_t *dest, *src; nuclear@9: #endif 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; i> 3) << 11) | ((green >> 2) << 5) | (red >> 3); nuclear@6: nuclear@6: for(k=0; kx * pbuf->y; nuclear@5: unsigned short *pixels = pbuf->pixels; nuclear@5: nuclear@9: #ifdef PALMODE nuclear@9: color |= color << 8; nuclear@9: sz /= 2; nuclear@9: #endif nuclear@9: nuclear@9: for(i=0; itype) { nuclear@5: case SDL_KEYDOWN: nuclear@5: case SDL_KEYUP: nuclear@18: handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0); 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@9: case '\r': 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@18: if(gba_key == -1) { nuclear@18: return; nuclear@18: } nuclear@18: nuclear@18: if(state) { nuclear@18: keystate |= gba_key; nuclear@18: } else { nuclear@18: keystate &= ~gba_key; nuclear@18: } nuclear@5: game_keyb(gba_key, state); nuclear@5: }