gba-x3dtest
diff src/main_sdl.c @ 5:850be43b3135
sdl version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 16 Jun 2014 22:01:45 +0300 |
parents | |
children | 73b5f2e5d18a |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main_sdl.c Mon Jun 16 22:01:45 2014 +0300 1.3 @@ -0,0 +1,182 @@ 1.4 +#include "config.h" 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 +#include <SDL.h> 1.8 +#include "gbasys.h" 1.9 +#include "game.h" 1.10 + 1.11 +static int proc_events(SDL_Event *ev); 1.12 +static void handle_keyboard(int key, int state); 1.13 + 1.14 +struct pixel_buffer *back_buffer, *front_buffer; 1.15 + 1.16 +static SDL_Surface *surf; 1.17 +static struct pixel_buffer bbuf; 1.18 +static unsigned int keystate; 1.19 + 1.20 +int main(void) 1.21 +{ 1.22 + int i, j; 1.23 + 1.24 + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE); 1.25 + 1.26 + if(!(surf = SDL_SetVideoMode(WIDTH * SDLSCALE, HEIGHT * SDLSCALE, 16, SDL_SWSURFACE))) { 1.27 + fprintf(stderr, "failed to initialize graphics\n"); 1.28 + return 1; 1.29 + } 1.30 + SDL_WM_SetCaption("trycatch", 0); 1.31 + 1.32 + bbuf.x = WIDTH; 1.33 + bbuf.y = HEIGHT; 1.34 + bbuf.bpp = 16; 1.35 + if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) { 1.36 + fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT); 1.37 + SDL_Quit(); 1.38 + return 1; 1.39 + } 1.40 + 1.41 + back_buffer = front_buffer = &bbuf; 1.42 + 1.43 + for(;;) { 1.44 + SDL_Event ev; 1.45 + uint16_t *dest, *src; 1.46 + 1.47 + while(SDL_PollEvent(&ev)) { 1.48 + if(proc_events(&ev) == -1) { 1.49 + goto done; 1.50 + } 1.51 + } 1.52 + 1.53 + game_draw(); 1.54 + 1.55 + if(SDL_MUSTLOCK(surf)) { 1.56 + SDL_LockSurface(surf); 1.57 + } 1.58 + 1.59 + src = bbuf.pixels; 1.60 + dest = surf->pixels; 1.61 + 1.62 + for(i=0; i<HEIGHT; i++) { 1.63 + for(j=0; j<WIDTH; j++) { 1.64 + dest[0] = dest[1] = dest[WIDTH] = dest[WIDTH + 1] = *src++; 1.65 + dest += 2; 1.66 + } 1.67 + dest += WIDTH; 1.68 + } 1.69 + 1.70 + if(SDL_MUSTLOCK(surf)) { 1.71 + SDL_UnlockSurface(surf); 1.72 + } 1.73 + keystate = 0; 1.74 + } 1.75 + 1.76 +done: 1.77 + SDL_Quit(); 1.78 + return 0; 1.79 +} 1.80 + 1.81 +void flip(void) 1.82 +{ 1.83 + SDL_Flip(surf); 1.84 +} 1.85 + 1.86 +void clear_buffer(struct pixel_buffer *pbuf, unsigned short color) 1.87 +{ 1.88 + int i; 1.89 + unsigned short *pixels = pbuf->pixels; 1.90 + 1.91 + for(i=0; i<pbuf->x * pbuf->y; i++) { 1.92 + *pixels++ = color; 1.93 + } 1.94 +} 1.95 + 1.96 +int get_key_state(int key) 1.97 +{ 1.98 + return keystate & key; 1.99 +} 1.100 + 1.101 +unsigned long get_millisec(void) 1.102 +{ 1.103 + return SDL_GetTicks(); 1.104 +} 1.105 + 1.106 +static int proc_events(SDL_Event *ev) 1.107 +{ 1.108 + switch(ev->type) { 1.109 + case SDL_KEYDOWN: 1.110 + case SDL_KEYUP: 1.111 + handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN); 1.112 + break; 1.113 + 1.114 + case SDL_QUIT: 1.115 + return -1; 1.116 + } 1.117 + 1.118 + return 0; 1.119 +} 1.120 + 1.121 +static void handle_keyboard(int key, int state) 1.122 +{ 1.123 + int gba_key = -1; 1.124 + 1.125 + switch(key) { 1.126 + case SDLK_ESCAPE: 1.127 + { 1.128 + SDL_Event ev; 1.129 + ev.type = SDL_QUIT; 1.130 + SDL_PushEvent(&ev); 1.131 + } 1.132 + break; 1.133 + 1.134 + case 'a': 1.135 + case 'A': 1.136 + case SDLK_LEFT: 1.137 + gba_key = KEY_LEFT; 1.138 + break; 1.139 + 1.140 + case 'd': 1.141 + case 'D': 1.142 + case SDLK_RIGHT: 1.143 + gba_key = KEY_RIGHT; 1.144 + break; 1.145 + 1.146 + case 'w': 1.147 + case 'W': 1.148 + case SDLK_UP: 1.149 + gba_key = KEY_UP; 1.150 + break; 1.151 + 1.152 + case 's': 1.153 + case 'S': 1.154 + case SDLK_DOWN: 1.155 + gba_key = KEY_DOWN; 1.156 + break; 1.157 + 1.158 + case '\n': 1.159 + gba_key = KEY_A; 1.160 + break; 1.161 + case '\b': 1.162 + gba_key = KEY_B; 1.163 + break; 1.164 + 1.165 + case ',': 1.166 + gba_key = KEY_SELECT; 1.167 + break; 1.168 + case '.': 1.169 + gba_key = KEY_START; 1.170 + break; 1.171 + 1.172 + case '[': 1.173 + gba_key = KEY_L; 1.174 + break; 1.175 + case ']': 1.176 + gba_key = KEY_R; 1.177 + break; 1.178 + 1.179 + default: 1.180 + return; 1.181 + } 1.182 + 1.183 + keystate |= gba_key; 1.184 + game_keyb(gba_key, state); 1.185 +}