gba-x3dtest

annotate src/main_sdl.c @ 19:62390f9cc93e

texture cache optimization failed
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 30 Jun 2014 09:07:41 +0300
parents 2070a81127f2
children
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@13 88 unsigned int red = GET_R(pixel);
nuclear@13 89 unsigned int green = GET_G(pixel);
nuclear@13 90 unsigned int blue = GET_B(pixel);
nuclear@13 91
nuclear@13 92 pixel = ((blue >> 3) << 11) | ((green >> 2) << 5) | (red >> 3);
nuclear@6 93
nuclear@6 94 for(k=0; k<sdlscale; k++) {
nuclear@6 95 for(l=0; l<sdlscale; l++) {
nuclear@6 96 dest[k * sdlscale * WIDTH + l] = pixel;
nuclear@6 97 }
nuclear@6 98 }
nuclear@6 99 dest += sdlscale;
nuclear@5 100 }
nuclear@6 101 dest += WIDTH * sdlscale * (sdlscale - 1);
nuclear@5 102 }
nuclear@5 103
nuclear@5 104 if(SDL_MUSTLOCK(surf)) {
nuclear@5 105 SDL_UnlockSurface(surf);
nuclear@5 106 }
nuclear@5 107 }
nuclear@5 108
nuclear@5 109 done:
nuclear@5 110 SDL_Quit();
nuclear@5 111 return 0;
nuclear@5 112 }
nuclear@5 113
nuclear@5 114 void flip(void)
nuclear@5 115 {
nuclear@5 116 SDL_Flip(surf);
nuclear@5 117 }
nuclear@5 118
nuclear@5 119 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
nuclear@5 120 {
nuclear@9 121 int i, sz = pbuf->x * pbuf->y;
nuclear@5 122 unsigned short *pixels = pbuf->pixels;
nuclear@5 123
nuclear@9 124 #ifdef PALMODE
nuclear@9 125 color |= color << 8;
nuclear@9 126 sz /= 2;
nuclear@9 127 #endif
nuclear@9 128
nuclear@9 129 for(i=0; i<sz; i++) {
nuclear@5 130 *pixels++ = color;
nuclear@5 131 }
nuclear@5 132 }
nuclear@5 133
nuclear@9 134 void set_palette(int idx, int r, int g, int b)
nuclear@9 135 {
nuclear@9 136 SDL_Color col;
nuclear@9 137 col.r = r;
nuclear@9 138 col.g = g;
nuclear@9 139 col.b = b;
nuclear@9 140
nuclear@9 141 if(SDL_SetPalette(surf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
nuclear@9 142 fprintf(stderr, "set_palette failed to set the required color\n");
nuclear@9 143 }
nuclear@9 144 }
nuclear@9 145
nuclear@9 146
nuclear@5 147 int get_key_state(int key)
nuclear@5 148 {
nuclear@5 149 return keystate & key;
nuclear@5 150 }
nuclear@5 151
nuclear@5 152 unsigned long get_millisec(void)
nuclear@5 153 {
nuclear@5 154 return SDL_GetTicks();
nuclear@5 155 }
nuclear@5 156
nuclear@5 157 static int proc_events(SDL_Event *ev)
nuclear@5 158 {
nuclear@5 159 switch(ev->type) {
nuclear@5 160 case SDL_KEYDOWN:
nuclear@5 161 case SDL_KEYUP:
nuclear@18 162 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
nuclear@5 163 break;
nuclear@5 164
nuclear@5 165 case SDL_QUIT:
nuclear@5 166 return -1;
nuclear@5 167 }
nuclear@5 168
nuclear@5 169 return 0;
nuclear@5 170 }
nuclear@5 171
nuclear@5 172 static void handle_keyboard(int key, int state)
nuclear@5 173 {
nuclear@5 174 int gba_key = -1;
nuclear@5 175
nuclear@5 176 switch(key) {
nuclear@5 177 case SDLK_ESCAPE:
nuclear@5 178 {
nuclear@5 179 SDL_Event ev;
nuclear@5 180 ev.type = SDL_QUIT;
nuclear@5 181 SDL_PushEvent(&ev);
nuclear@5 182 }
nuclear@5 183 break;
nuclear@5 184
nuclear@5 185 case 'a':
nuclear@5 186 case 'A':
nuclear@5 187 case SDLK_LEFT:
nuclear@5 188 gba_key = KEY_LEFT;
nuclear@5 189 break;
nuclear@5 190
nuclear@5 191 case 'd':
nuclear@5 192 case 'D':
nuclear@5 193 case SDLK_RIGHT:
nuclear@5 194 gba_key = KEY_RIGHT;
nuclear@5 195 break;
nuclear@5 196
nuclear@5 197 case 'w':
nuclear@5 198 case 'W':
nuclear@5 199 case SDLK_UP:
nuclear@5 200 gba_key = KEY_UP;
nuclear@5 201 break;
nuclear@5 202
nuclear@5 203 case 's':
nuclear@5 204 case 'S':
nuclear@5 205 case SDLK_DOWN:
nuclear@5 206 gba_key = KEY_DOWN;
nuclear@5 207 break;
nuclear@5 208
nuclear@5 209 case '\n':
nuclear@9 210 case '\r':
nuclear@5 211 gba_key = KEY_A;
nuclear@5 212 break;
nuclear@5 213 case '\b':
nuclear@5 214 gba_key = KEY_B;
nuclear@5 215 break;
nuclear@5 216
nuclear@5 217 case ',':
nuclear@5 218 gba_key = KEY_SELECT;
nuclear@5 219 break;
nuclear@5 220 case '.':
nuclear@5 221 gba_key = KEY_START;
nuclear@5 222 break;
nuclear@5 223
nuclear@5 224 case '[':
nuclear@5 225 gba_key = KEY_L;
nuclear@5 226 break;
nuclear@5 227 case ']':
nuclear@5 228 gba_key = KEY_R;
nuclear@5 229 break;
nuclear@5 230
nuclear@5 231 default:
nuclear@5 232 return;
nuclear@5 233 }
nuclear@5 234
nuclear@18 235 if(gba_key == -1) {
nuclear@18 236 return;
nuclear@18 237 }
nuclear@18 238
nuclear@18 239 if(state) {
nuclear@18 240 keystate |= gba_key;
nuclear@18 241 } else {
nuclear@18 242 keystate &= ~gba_key;
nuclear@18 243 }
nuclear@5 244 game_keyb(gba_key, state);
nuclear@5 245 }