gba-trycatch

annotate src/main_sdl.c @ 8:fb0a0d6a8b52

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