Fixed widescreen editor video.
[cantaveria.git] / video.c
blobcb7755a4313f5256bee8384869e161043d4e53a9
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
25 video module is the low level interface to video output.
26 it has several low level functions.
27 * initialize the video system for a certain video mode
28 * load graphics data into the abstract graphics pool
29 * draw a portion of some graphics somewhere on screen
30 * millisecond time delta generator
31 * screen clear
32 * screen flip
33 * process suspend
39 #include <math.h>
41 #include <SDL/SDL.h>
42 #include <SDL/SDL_opengl.h>
44 #include <list.h>
45 #include <root.h>
46 #include <util.h>
47 #include <video.h>
48 #include <loader.h>
50 SDL_Surface* video;
51 int gl_flag = 0;
52 int fullscreen = 0;
53 int W, H;
56 static int time_last; /* initialized by backend_init */
57 int since(){
58 int time_now = SDL_GetTicks();
59 int diff = time_now - time_last;
60 time_last = time_now;
61 return diff;
64 void delay(int ms){
65 SDL_Delay(ms);
75 struct {
76 char* filename;
77 SDL_Surface* surf;
78 GLuint texture;
79 int w;
80 int h;
81 } gfx[MAX_GFX];
82 int gfx_count = 0;
84 int screen_w = 320;
85 int screen_h = 240;
86 int screen_offset_x = 0;
87 int screen_offset_y = 0;
92 /********************/
93 /* drawing routines */
94 /********************/
96 void draw_gfx_raw(int gfxid, int x, int y, int X, int Y, int W, int H){
97 if(!gl_flag){
98 SDL_Surface* surf = gfx[gfxid].surf;
99 SDL_Rect r1 = {X,Y,W,H};
100 SDL_Rect r2 = {x,y,W,H};
101 SDL_BlitSurface(surf,&r1,video,&r2);
103 else{
104 double w = gfx[gfxid].w;
105 double h = gfx[gfxid].h;
106 double X0 = ((double)X)/w;
107 double X1 = X0 + ((double)W)/w;
108 double Y0 = ((double)Y)/h;
109 double Y1 = Y0 + ((double)H)/h;
111 glBindTexture( GL_TEXTURE_2D, gfx[gfxid].texture );
112 glBegin( GL_QUADS );
113 glTexCoord2d(X0,Y0);
114 glVertex3f(x,y,0);
116 glTexCoord2d(X1,Y0);
117 glVertex3f(x+W,y,0);
119 glTexCoord2d(X1,Y1);
120 glVertex3f(x+W,y+H,0);
122 glTexCoord2d(X0,Y1);
123 glVertex3f(x,y+H,0);
124 glEnd();
128 void draw_test_rect(int x, int y, int w, int h){
129 if(!gl_flag){
130 SDL_Rect dst = {x, y, w, h};
131 SDL_FillRect(video, &dst, 0x00ff00ff);
133 else{
134 glColor3f(1.0, 0.0, 1.0);
135 glBegin( GL_QUADS );
136 glVertex3f(x, y, 0);
137 glVertex3f(x+w, y, 0);
138 glVertex3f(x+w, y+h, 0);
139 glVertex3f(x, y+h, 0);
140 glEnd();
141 glColor3f(1.0,1.0,1.0);
145 void draw_gfx(int gfxid, int x, int y, int X, int Y, int W, int H){
146 draw_gfx_raw(gfxid, x+screen_offset_x, y+screen_offset_y, X, Y, W, H);
149 void clear_video(){
150 if(!gl_flag){
151 SDL_FillRect(video, 0, 0);
153 else{
154 glClearColor(0.0,0.0,0.0,0.0);
155 glClear( GL_COLOR_BUFFER_BIT );
159 void update_video(){
160 if(!gl_flag){
161 SDL_UpdateRect(video,0,0,0,0);
163 else{
164 SDL_GL_SwapBuffers();
169 /********************/
170 /* graphics loading */
171 /********************/
173 SDL_Surface* SDL_NewSurface(int w, int h){
174 char prefix[32] = "SDL_NewSurface";
175 SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY,w,h,32,0,0,0,0);
176 if(!tmp){
177 out_of_memory(prefix);
180 SDL_Surface* surf = SDL_DisplayFormat(tmp);
181 if(!surf){
182 out_of_memory(prefix);
185 SDL_FreeSurface(tmp);
187 SDL_FillRect(surf,0,0x00ffffff);
189 return surf;
192 void debug_surf(char* msg, SDL_Surface* surf){
193 printf("%s\n", msg);
194 int i;
195 unsigned char* ptr = surf->pixels;
196 int bpp = surf->format->BytesPerPixel;
198 for(i=0; i<500; i+=bpp){
199 printf("%2x %2x %2x %2x\n", ptr[i], ptr[i+1], ptr[i+2], ptr[i+3]);
205 SDL_Surface* load_tga(char* filename){
206 char path[256] = "gfx/";
207 unsigned char header[18];
208 strmcat(path, filename, 256);
209 reader* rd = loader_open(path);
210 int w, h, bpp;
211 int mask;
212 unsigned char parts[4];
213 int key = 0;
214 int i;
215 int npix;
216 Uint32* pixels;
218 if(!rd){
219 error_msg("load_pixmap: error opening file\n");
220 return NULL;
223 if(loader_read(rd, header, 18) < 0){
224 error_msg("load_pixmap: error reading pixmap header\n");
225 loader_close(rd);
226 return NULL;
229 w = header[12] + (header[13]<<8);
230 h = header[14] + (header[15]<<8);
231 bpp = header[16];
233 npix = w*h;
235 SDL_Surface* surf = SDL_CreateRGBSurface(0,w,h,32,
236 0x00ff0000,0x0000ff00,0x000000ff,0xff000000);
237 if(!surf){
238 out_of_memory("load_pixmap");
241 pixels = surf->pixels;
242 mask = surf->format->Amask;
244 for(i=0; i<npix; i++){
245 if(loader_read(rd, parts, bpp/8) < 0){
246 error_msg("load_pixmap: error reading pixmap data\n");
247 loader_close(rd);
248 SDL_FreeSurface(surf);
249 return NULL;
252 if(bpp == 24){
253 if(parts[0] == 0 && parts[1] == 0 && parts[2] == 0){
254 pixels[i] = 0;
256 else{
257 pixels[i] = parts[0] | (parts[1]<<8) | (parts[2]<<16);
258 pixels[i] |= mask;
261 else if(bpp == 32){
262 if(parts[1] == 0 && parts[2] == 0 && parts[3] == 0){
263 pixels[i] = 0;
265 else{
266 pixels[i] = parts[0] | (parts[1]<<8) | (parts[2]<<16);
267 pixels[i] |= mask;
272 loader_close(rd);
274 SDL_SetAlpha(surf, 0, 0);
275 SDL_SetColorKey(surf, SDL_SRCCOLORKEY, key);
276 //debug_surf(filename, surf);
278 return surf;
282 int load_gfx(char* filename){
283 int i;
284 SDL_Surface* src;
286 if(gfx_count == MAX_GFX){
287 fatal_error(
288 "load_gfx: cannot load any more than %d graphics\n",
289 MAX_GFX
293 for(i=0; i<gfx_count; i++){/*check for already loaded gfx*/
294 if(strcmp(gfx[i].filename, filename)==0){
295 return i;
299 src = load_tga(filename);
300 if(!src){
301 fatal_error("load_gfx: failed to load %s\n",filename);
304 if(!gl_flag){
305 gfx[gfx_count].filename = strxcpy(filename);
306 gfx[gfx_count].surf = src;
307 gfx[gfx_count].w = src->w;
308 gfx[gfx_count].h = src->h;
310 else {
311 GLuint texture;
313 glGenTextures( 1, &texture );
314 glBindTexture( GL_TEXTURE_2D, texture );
316 glTexParameteri(
317 GL_TEXTURE_2D,
318 GL_TEXTURE_MIN_FILTER,
319 GL_NEAREST
322 glTexParameteri(
323 GL_TEXTURE_2D,
324 GL_TEXTURE_MAG_FILTER,
325 GL_NEAREST
328 glTexParameteri(
329 GL_TEXTURE_2D,
330 GL_TEXTURE_WRAP_S,
331 GL_REPEAT
334 glTexParameteri(
335 GL_TEXTURE_2D,
336 GL_TEXTURE_WRAP_T,
337 GL_REPEAT
340 glTexImage2D(
341 GL_TEXTURE_2D,
342 0, 4,
343 src->w, src->h,
345 GL_BGRA, GL_UNSIGNED_BYTE,
346 src->pixels
349 gfx[gfx_count].filename = strxcpy(filename);
350 gfx[gfx_count].texture = texture;
351 gfx[gfx_count].w = src->w;
352 gfx[gfx_count].h = src->h;
354 SDL_FreeSurface(src);
357 return gfx_count++;
360 int gfx_width(int gfxid){
361 return gfx[gfxid].w;
364 int gfx_height(int gfxid){
365 return gfx[gfxid].h;
373 /******************/
374 /* initialization */
375 /******************/
377 void sdl_init(){
378 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK)==-1){
379 error_msg("sdl: %s\n",SDL_GetError());
380 exit(-1);
387 void print_version(){
388 char message[] =
389 "This program is distributed under the terms of the GNU General\n"
390 "Public License (v2) and comes with ABSOLUTELY NO WARRANTY.\n\n"
392 "Send questions, comments, and bugs to evanrinehart@gmail.com\n\n"
394 "Send money to:\n"
395 "1850 Claiborne St\n"
396 "Mandeville, LA 70448\n"
397 "United States of America\n\n"
399 "Thanks! :)\n"
402 printf("Cantaveria (v%d.%d)\n", VERSION_MAJOR, VERSION_MINOR);
403 printf("Copyright 2009 Evan Rinehart\n\n");
404 printf(message);
407 void print_help(){
408 printf("options:\n");
409 printf(" -gl use opengl video mode\n");
410 printf(" -f use fullscreen video mode\n");
411 printf(" -h print this help\n");
412 printf(" -v print version info\n");
415 void parse_options(int argc, char* argv[], int* fullscreen, int* gl_flag){
416 int i;
417 for(i=0; i<argc; i++){
418 if(!strcmp(argv[i], "-gl")){
419 *gl_flag = 1;
420 continue;
422 if(!strcmp(argv[i], "-f")){
423 *fullscreen = 1;
425 if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")){
426 print_help();
427 exit(0);
429 if(!strcmp(argv[i], "-v")){
430 print_version();
431 exit(0);
437 void setup_joysticks(){
438 int N = SDL_NumJoysticks();
439 int i;
440 boot_msg("sdl: detected %d joysticks\n", N);
441 for(i=0; i<N; i++){
442 if(SDL_JoystickOpen(i)){
443 boot_msg(" joy%d: %s\n", i, SDL_JoystickName(i));
445 else{
446 boot_msg(" joy%d: %s (failed to open)\n", i, SDL_JoystickName(i));
452 void setup_video(){
453 int flags = 0;
454 int i;
456 SDL_WM_SetCaption("cantaveria","cantaveria");
457 SDL_ShowCursor(SDL_DISABLE);
458 const SDL_VideoInfo* vinfo = SDL_GetVideoInfo();
461 if(fullscreen && gl_flag){
462 W = vinfo->current_w;
463 H = vinfo->current_h;
465 else if(gl_flag){
466 //W = 320;
467 //H = 240;
468 W = 640;
469 H = 480;
470 //W = 960;
471 //H = 720;
473 else if(fullscreen){
474 W = 320;
475 H = 240;
477 else{
478 W = 320;
479 H = 240;
482 if(gl_flag){
483 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
484 //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
485 //SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
486 flags |= SDL_OPENGL;
489 if(fullscreen){
490 flags |= SDL_FULLSCREEN;
495 video = SDL_SetVideoMode(W,H,32,flags);
496 if(video == NULL){
497 fatal_error("sdl: %s\n",SDL_GetError());
500 if(gl_flag){
501 glEnable(GL_BLEND);
502 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
504 glEnable( GL_TEXTURE_2D );
505 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
507 glViewport( 0, 0, W, H );
509 glClear( GL_COLOR_BUFFER_BIT );
511 glMatrixMode( GL_PROJECTION );
512 glLoadIdentity();
513 if(fullscreen){
514 //glOrtho(0.0f, 240*aspect, 240, 0.0f, -1.0f, 1.0f);
515 //glOrtho(0.0f, 1280.0/3, 800.0/3, 0.0f, -1.0f, 1.0f);
516 int min = 9999;
517 int n = 0;
518 for(i=1; i<10; i++){
519 if(abs(H/i - 240) < min){ min = H/i - 240; n = i; }
521 double new_w = ((double)W)/n;
522 double new_h = ((double)H)/n;
523 screen_offset_x = (new_w-320)/2;
524 screen_offset_y = (new_h-240)/2;
525 glOrtho(0.0f, new_w, new_h, 0.0f, -1.0f, 1.0f);
526 screen_w = new_w;
527 screen_h = new_h;
529 else{
530 glOrtho(0.0f, 320, 240, 0.0f, -1.0f, 1.0f);
531 screen_w = 320;
532 screen_h = 240;
535 glMatrixMode( GL_MODELVIEW );
536 glLoadIdentity();
538 else{
539 screen_w = 320;
540 screen_h = 240;
543 boot_msg("video:\n");
544 boot_msg(" resolution: %d x %d %s\n",W,H,fullscreen?"fullscreen":"windowed");
545 boot_msg(" pixel dimensions: %d x %d\n",screen_w,screen_h);
546 boot_msg(" aspect ratio: %g\n",((double)W)/H);
547 boot_msg(" opengl: %s\n",gl_flag?"yes":"no");
548 boot_msg(" x-offset: %d\n",screen_offset_x);
549 boot_msg(" y-offset: %d\n",screen_offset_y);
550 boot_msg(" video on\n");
554 void map_pixel(int mx, int my, int *x, int *y){
555 *x = mx*screen_w/W;
556 *y = my*screen_h/H;
563 void video_init(int argc, char* argv[]){
564 sdl_init();
565 parse_options(argc, argv, &fullscreen, &gl_flag);
566 setup_joysticks();
567 setup_video();
568 time_last = SDL_GetTicks();
571 void video_quit(){
572 boot_msg("sdl: quit\n");
573 SDL_Quit();
577 /* fps */
579 int fps = 0;
580 int update_count = 0;
581 int draw_count = 0;
583 void fps_update(){
584 update_count++;
585 if(update_count == 100){
586 fps = draw_count * 100 / update_count;
587 update_count = 0;
588 draw_count = 0;
592 void fps_draw(){
593 draw_count++;
596 int get_fps(){
597 return fps;