2010-10-12 10:55:23 +02:00
|
|
|
/*
|
2011-03-05 13:44:21 +01:00
|
|
|
* This software is licensed under the terms of the MIT-License
|
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
|
|
|
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
2010-10-12 10:55:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "global.h"
|
2010-10-18 14:40:15 +02:00
|
|
|
#include <SDL/SDL.h>
|
2011-04-29 10:26:37 +02:00
|
|
|
#include <time.h>
|
2011-07-02 18:36:08 +02:00
|
|
|
#include <stdio.h>
|
2011-07-03 19:49:52 +02:00
|
|
|
#include "paths/native.h"
|
2011-07-04 09:14:08 +02:00
|
|
|
#include "resource/resource.h"
|
2010-10-12 10:55:23 +02:00
|
|
|
|
|
|
|
Global global;
|
|
|
|
|
2011-07-04 11:09:41 +02:00
|
|
|
void init_global() {
|
2011-04-24 15:39:17 +02:00
|
|
|
memset(&global, 0, sizeof(global));
|
2011-04-29 10:26:37 +02:00
|
|
|
srand(time(0));
|
2011-06-27 13:36:35 +02:00
|
|
|
|
2011-04-02 12:14:37 +02:00
|
|
|
load_resources();
|
2011-06-27 08:20:42 +02:00
|
|
|
printf("- fonts:\n");
|
2011-03-19 16:21:48 +01:00
|
|
|
init_fonts();
|
2011-04-25 19:40:21 +02:00
|
|
|
}
|
|
|
|
|
2010-10-17 09:27:44 +02:00
|
|
|
void game_over() {
|
2011-06-25 12:41:40 +02:00
|
|
|
global.game_over = GAMEOVER_DEFEAT;
|
2011-03-17 21:13:11 +01:00
|
|
|
printf("Game Over!\n");
|
2010-10-12 18:17:15 +02:00
|
|
|
}
|
2010-10-18 14:40:15 +02:00
|
|
|
|
2011-06-25 12:41:40 +02:00
|
|
|
void frame_rate(int *lasttime) {
|
|
|
|
int t = *lasttime + 1000/FPS - SDL_GetTicks();
|
2010-10-18 14:40:15 +02:00
|
|
|
if(t > 0)
|
|
|
|
SDL_Delay(t);
|
2011-03-05 19:03:32 +01:00
|
|
|
|
2011-06-25 12:41:40 +02:00
|
|
|
*lasttime = SDL_GetTicks();
|
2011-06-13 18:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void calc_fps(FPSCounter *fps) {
|
|
|
|
if(SDL_GetTicks() > fps->fpstime+1000) {
|
|
|
|
fps->show_fps = fps->fps;
|
|
|
|
fps->fps = 0;
|
|
|
|
fps->fpstime = SDL_GetTicks();
|
|
|
|
} else {
|
|
|
|
fps->fps++;
|
|
|
|
}
|
2011-06-24 12:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_ortho() {
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2011-06-24 19:16:05 +02:00
|
|
|
glOrtho(0, SCREEN_W, SCREEN_H, 0, -100, 100);
|
2011-06-24 12:35:03 +02:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2011-06-28 19:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline double frand() {
|
|
|
|
return rand()/(double)RAND_MAX;
|
2011-07-02 15:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern SDL_Surface *display;
|
|
|
|
|
|
|
|
void toggle_fullscreen()
|
|
|
|
{
|
|
|
|
int newflags = display->flags;
|
|
|
|
newflags ^= SDL_FULLSCREEN;
|
2011-07-03 18:46:40 +02:00
|
|
|
tconfig.intval[FULLSCREEN] = newflags & SDL_FULLSCREEN;
|
2011-07-02 15:28:38 +02:00
|
|
|
|
|
|
|
SDL_FreeSurface(display);
|
|
|
|
if((display = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 32, newflags)) == NULL)
|
|
|
|
errx(-1, "Error opening screen: %s", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
2011-07-02 18:36:08 +02:00
|
|
|
void take_screenshot()
|
2011-07-02 15:28:38 +02:00
|
|
|
{
|
2011-07-02 18:36:08 +02:00
|
|
|
FILE *out;
|
|
|
|
char data[3 * SCREEN_W * SCREEN_H];
|
|
|
|
short head[] = {0, 2, 0, 0, 0, 0, SCREEN_W, SCREEN_H, 24};
|
2011-07-03 19:44:34 +02:00
|
|
|
char outfile[128], *outpath;
|
2011-07-02 18:36:08 +02:00
|
|
|
time_t rawtime;
|
|
|
|
struct tm * timeinfo;
|
|
|
|
|
|
|
|
time(&rawtime);
|
|
|
|
timeinfo = localtime(&rawtime);
|
2011-07-03 20:14:53 +02:00
|
|
|
strftime(outfile, 128, "/taisei_%Y%m%d_%H-%M-%S_%Z.tga", timeinfo);
|
2011-07-03 19:44:34 +02:00
|
|
|
|
2011-07-03 19:49:52 +02:00
|
|
|
outpath = malloc(strlen(outfile) + strlen(get_screenshots_path()) + 1);
|
|
|
|
strcpy(outpath, get_screenshots_path());
|
2011-07-02 18:36:08 +02:00
|
|
|
strcat(outpath, outfile);
|
|
|
|
|
|
|
|
printf("Saving screenshot as %s\n", outpath);
|
|
|
|
out = fopen(outpath, "w");
|
2011-07-03 19:44:34 +02:00
|
|
|
free(outpath);
|
|
|
|
|
2011-07-02 18:36:08 +02:00
|
|
|
if(!out)
|
|
|
|
{
|
|
|
|
perror("fopen");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glReadBuffer(GL_FRONT);
|
|
|
|
glReadPixels(0, 0, SCREEN_W, SCREEN_H, GL_BGR, GL_UNSIGNED_BYTE, data);
|
|
|
|
fwrite(&head, sizeof(head), 1, out);
|
|
|
|
fwrite(data, 3 * SCREEN_W * SCREEN_H, 1, out);
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2011-07-02 20:13:21 +02:00
|
|
|
void global_processevent(SDL_Event *event)
|
2011-07-02 15:28:38 +02:00
|
|
|
{
|
2011-07-02 20:13:21 +02:00
|
|
|
int sym = event->key.keysym.sym;
|
|
|
|
Uint8 *keys;
|
|
|
|
|
2011-07-04 09:14:08 +02:00
|
|
|
keys = SDL_GetKeyState(NULL);
|
|
|
|
|
2011-07-02 20:13:21 +02:00
|
|
|
if(event->type == SDL_KEYDOWN)
|
|
|
|
{
|
|
|
|
if(sym == tconfig.intval[KEY_SCREENSHOT])
|
|
|
|
take_screenshot();
|
2011-07-04 09:14:08 +02:00
|
|
|
if((sym == SDLK_RETURN && (keys[SDLK_LALT] || keys[SDLK_RALT])) || sym == tconfig.intval[KEY_FULLSCREEN])
|
2011-07-02 15:28:38 +02:00
|
|
|
toggle_fullscreen();
|
|
|
|
}
|
|
|
|
}
|