gba-trycatch

annotate src/main_sdl.c @ 11:c47e54796b82

added a simple README
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Jun 2014 07:54:29 +0300
parents fb0a0d6a8b52
children 2070a81127f2
rev   line source
nuclear@5 1 #include "config.h"
nuclear@5 2 #include <stdio.h>
nuclear@5 3 #include <stdlib.h>
nuclear@5 4 #include <SDL.h>
nuclear@5 5 #include "gbasys.h"
nuclear@5 6 #include "game.h"
nuclear@5 7
nuclear@9 8 #ifdef PALMODE
nuclear@9 9 #define BPP 8
nuclear@9 10 #define PIXSZ 1
nuclear@9 11 #else
nuclear@9 12 #define BPP 16
nuclear@9 13 #define PIXSZ 2
nuclear@9 14 #endif
nuclear@9 15
nuclear@5 16 static int proc_events(SDL_Event *ev);
nuclear@5 17 static void handle_keyboard(int key, int state);
nuclear@5 18
nuclear@5 19 struct pixel_buffer *back_buffer, *front_buffer;
nuclear@5 20
nuclear@5 21 static SDL_Surface *surf;
nuclear@5 22 static struct pixel_buffer bbuf;
nuclear@5 23 static unsigned int keystate;
nuclear@6 24 static int sdlscale = 2;
nuclear@5 25
nuclear@5 26 int main(void)
nuclear@5 27 {
nuclear@6 28 int i, j, k, l;
nuclear@6 29 char *env;
nuclear@6 30
nuclear@6 31 if((env = getenv("SDLSCALE"))) {
nuclear@6 32 if(!(sdlscale = atoi(env))) {
nuclear@6 33 fprintf(stderr, "invalid SDLSCALE envvar value (%s)\n", env);
nuclear@6 34 sdlscale = 2;
nuclear@6 35 }
nuclear@6 36 }
nuclear@5 37
nuclear@5 38 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
nuclear@5 39
nuclear@9 40 if(!(surf = SDL_SetVideoMode(WIDTH * sdlscale, HEIGHT * sdlscale, BPP, SDL_SWSURFACE))) {
nuclear@5 41 fprintf(stderr, "failed to initialize graphics\n");
nuclear@5 42 return 1;
nuclear@5 43 }
nuclear@5 44 SDL_WM_SetCaption("trycatch", 0);
nuclear@5 45
nuclear@5 46 bbuf.x = WIDTH;
nuclear@5 47 bbuf.y = HEIGHT;
nuclear@9 48 bbuf.bpp = BPP;
nuclear@9 49 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * PIXSZ))) {
nuclear@5 50 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
nuclear@5 51 SDL_Quit();
nuclear@5 52 return 1;
nuclear@5 53 }
nuclear@5 54
nuclear@5 55 back_buffer = front_buffer = &bbuf;
nuclear@5 56
nuclear@8 57 if(game_init() == -1) {
nuclear@8 58 SDL_Quit();
nuclear@8 59 return 1;
nuclear@8 60 }
nuclear@8 61
nuclear@5 62 for(;;) {
nuclear@5 63 SDL_Event ev;
nuclear@9 64 #ifdef PALMODE
nuclear@9 65 uint8_t *dest, *src;
nuclear@9 66 #else
nuclear@5 67 uint16_t *dest, *src;
nuclear@9 68 #endif
nuclear@5 69
nuclear@5 70 while(SDL_PollEvent(&ev)) {
nuclear@5 71 if(proc_events(&ev) == -1) {
nuclear@5 72 goto done;
nuclear@5 73 }
nuclear@5 74 }
nuclear@5 75
nuclear@5 76 game_draw();
nuclear@5 77
nuclear@5 78 if(SDL_MUSTLOCK(surf)) {
nuclear@5 79 SDL_LockSurface(surf);
nuclear@5 80 }
nuclear@5 81
nuclear@5 82 src = bbuf.pixels;
nuclear@5 83 dest = surf->pixels;
nuclear@5 84
nuclear@5 85 for(i=0; i<HEIGHT; i++) {
nuclear@5 86 for(j=0; j<WIDTH; j++) {
nuclear@6 87 uint16_t pixel = *src++;
nuclear@6 88
nuclear@6 89 for(k=0; k<sdlscale; k++) {
nuclear@6 90 for(l=0; l<sdlscale; l++) {
nuclear@6 91 dest[k * sdlscale * WIDTH + l] = pixel;
nuclear@6 92 }
nuclear@6 93 }
nuclear@6 94 dest += sdlscale;
nuclear@5 95 }
nuclear@6 96 dest += WIDTH * sdlscale * (sdlscale - 1);
nuclear@5 97 }
nuclear@5 98
nuclear@5 99 if(SDL_MUSTLOCK(surf)) {
nuclear@5 100 SDL_UnlockSurface(surf);
nuclear@5 101 }
nuclear@5 102 keystate = 0;
nuclear@5 103 }
nuclear@5 104
nuclear@5 105 done:
nuclear@5 106 SDL_Quit();
nuclear@5 107 return 0;
nuclear@5 108 }
nuclear@5 109
nuclear@5 110 void flip(void)
nuclear@5 111 {
nuclear@5 112 SDL_Flip(surf);
nuclear@5 113 }
nuclear@5 114
nuclear@5 115 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
nuclear@5 116 {
nuclear@9 117 int i, sz = pbuf->x * pbuf->y;
nuclear@5 118 unsigned short *pixels = pbuf->pixels;
nuclear@5 119
nuclear@9 120 #ifdef PALMODE
nuclear@9 121 color |= color << 8;
nuclear@9 122 sz /= 2;
nuclear@9 123 #endif
nuclear@9 124
nuclear@9 125 for(i=0; i<sz; i++) {
nuclear@5 126 *pixels++ = color;
nuclear@5 127 }
nuclear@5 128 }
nuclear@5 129
nuclear@9 130 void set_palette(int idx, int r, int g, int b)
nuclear@9 131 {
nuclear@9 132 SDL_Color col;
nuclear@9 133 col.r = r;
nuclear@9 134 col.g = g;
nuclear@9 135 col.b = b;
nuclear@9 136
nuclear@9 137 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
nuclear@9 138 fprintf(stderr, "set_palette failed to set the required color\n");
nuclear@9 139 }
nuclear@9 140 }
nuclear@9 141
nuclear@9 142
nuclear@5 143 int get_key_state(int key)
nuclear@5 144 {
nuclear@5 145 return keystate & key;
nuclear@5 146 }
nuclear@5 147
nuclear@5 148 unsigned long get_millisec(void)
nuclear@5 149 {
nuclear@5 150 return SDL_GetTicks();
nuclear@5 151 }
nuclear@5 152
nuclear@5 153 static int proc_events(SDL_Event *ev)
nuclear@5 154 {
nuclear@5 155 switch(ev->type) {
nuclear@5 156 case SDL_KEYDOWN:
nuclear@5 157 case SDL_KEYUP:
nuclear@9 158 handle_keyboard(ev->key.keysym.sym, ev->key.state);
nuclear@5 159 break;
nuclear@5 160
nuclear@5 161 case SDL_QUIT:
nuclear@5 162 return -1;
nuclear@5 163 }
nuclear@5 164
nuclear@5 165 return 0;
nuclear@5 166 }
nuclear@5 167
nuclear@5 168 static void handle_keyboard(int key, int state)
nuclear@5 169 {
nuclear@5 170 int gba_key = -1;
nuclear@5 171
nuclear@5 172 switch(key) {
nuclear@5 173 case SDLK_ESCAPE:
nuclear@5 174 {
nuclear@5 175 SDL_Event ev;
nuclear@5 176 ev.type = SDL_QUIT;
nuclear@5 177 SDL_PushEvent(&ev);
nuclear@5 178 }
nuclear@5 179 break;
nuclear@5 180
nuclear@5 181 case 'a':
nuclear@5 182 case 'A':
nuclear@5 183 case SDLK_LEFT:
nuclear@5 184 gba_key = KEY_LEFT;
nuclear@5 185 break;
nuclear@5 186
nuclear@5 187 case 'd':
nuclear@5 188 case 'D':
nuclear@5 189 case SDLK_RIGHT:
nuclear@5 190 gba_key = KEY_RIGHT;
nuclear@5 191 break;
nuclear@5 192
nuclear@5 193 case 'w':
nuclear@5 194 case 'W':
nuclear@5 195 case SDLK_UP:
nuclear@5 196 gba_key = KEY_UP;
nuclear@5 197 break;
nuclear@5 198
nuclear@5 199 case 's':
nuclear@5 200 case 'S':
nuclear@5 201 case SDLK_DOWN:
nuclear@5 202 gba_key = KEY_DOWN;
nuclear@5 203 break;
nuclear@5 204
nuclear@5 205 case '\n':
nuclear@9 206 case '\r':
nuclear@5 207 gba_key = KEY_A;
nuclear@5 208 break;
nuclear@5 209 case '\b':
nuclear@5 210 gba_key = KEY_B;
nuclear@5 211 break;
nuclear@5 212
nuclear@5 213 case ',':
nuclear@5 214 gba_key = KEY_SELECT;
nuclear@5 215 break;
nuclear@5 216 case '.':
nuclear@5 217 gba_key = KEY_START;
nuclear@5 218 break;
nuclear@5 219
nuclear@5 220 case '[':
nuclear@5 221 gba_key = KEY_L;
nuclear@5 222 break;
nuclear@5 223 case ']':
nuclear@5 224 gba_key = KEY_R;
nuclear@5 225 break;
nuclear@5 226
nuclear@5 227 default:
nuclear@5 228 return;
nuclear@5 229 }
nuclear@5 230
nuclear@5 231 keystate |= gba_key;
nuclear@5 232 game_keyb(gba_key, state);
nuclear@5 233 }