taisei/src/global.c

265 lines
5.6 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
*/
#include "global.h"
2012-07-28 22:53:53 +02:00
#include "video.h"
2011-04-29 10:26:37 +02:00
#include <time.h>
#include <stdio.h>
2012-01-14 16:16:07 +01:00
#include <png.h>
#include "paths/native.h"
#include "resource/resource.h"
#include "taisei_err.h"
#include "replay.h"
Global global;
void init_global(void) {
memset(&global, 0, sizeof(global));
2012-07-27 19:11:45 +02:00
tsrand_seed_p(&global.rand_game, time(0));
tsrand_seed_p(&global.rand_visual, time(0));
tsrand_switch(&global.rand_visual);
memset(&resources, 0, sizeof(Resources));
2017-01-24 14:40:57 +01:00
printf("- fonts:\n");
init_fonts();
memset(&global.replay, 0, sizeof(Replay));
global.replaymode = REPLAY_RECORD;
char *e = getenv("TAISEI_SANIC");
if(e) {
global.frameskip = atoi(e);
if(global.frameskip < 0) {
global.frameskip = INT_MAX;
}
if(global.frameskip) {
warnx("FPS limiter disabled by environment. Gotta go fast! (frameskip = %i)", global.frameskip);
}
}
}
void print_state_checksum(void) {
2012-08-07 21:27:32 +02:00
int plr, spos = 0, smisc = 0, sargs = 0, proj = 0;
Player *p = &global.plr;
Enemy *s;
Projectile *pr;
plr = creal(p->pos)+cimag(p->pos)+p->focus+p->fire+p->power+p->lifes+p->bombs+p->recovery+p->deathtime+p->continues+p->moveflags;
for(s = global.plr.slaves; s; s = s->next) {
spos += creal(s->pos + s->pos0) + cimag(s->pos + s->pos0);
smisc += s->birthtime + s->hp + s->unbombable + s->alpha;
sargs += cabs(s->args[0]) + cabs(s->args[1]) + cabs(s->args[2]) + cabs(s->args[3]) + s->alpha;
}
for(pr = global.projs; pr; pr = pr->next)
proj += cabs(pr->pos + pr->pos0) + pr->birthtime + pr->angle + pr->type + cabs(pr->args[0]) + cabs(pr->args[1]) + cabs(pr->args[2]) + cabs(pr->args[3]);
printf("[%05d] %d\t(%d\t%d\t%d)\t%d\n", global.frames, plr, spos, smisc, sargs, proj);
}
void frame_rate(int *lasttime) {
if(global.frameskip) {
return;
}
int t = *lasttime + 1000.0/FPS - SDL_GetTicks();
if(t > 0)
SDL_Delay(t);
*lasttime = SDL_GetTicks();
}
2012-07-29 13:11:44 +02:00
double approach(double v, double t, double d) {
if(v < t) {
v += d;
if(v > t)
return t;
} else if(v > t) {
v -= d;
if(v < t)
return t;
}
return v;
}
void calc_fps(FPSCounter *fps) {
2012-07-29 13:11:44 +02:00
if(!fps->stagebg_fps)
fps->stagebg_fps = FPS;
if(SDL_GetTicks() > fps->fpstime+1000) {
fps->show_fps = fps->fps;
fps->fps = 0;
fps->fpstime = SDL_GetTicks();
} else {
fps->fps++;
}
2012-07-29 13:11:44 +02:00
fps->stagebg_fps = approach(fps->stagebg_fps, fps->show_fps, 0.1);
2011-06-24 12:35:03 +02:00
}
void set_ortho(void) {
2011-06-24 12:35:03 +02:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_W, SCREEN_H, 0, -100, 100);
2011-06-24 12:35:03 +02:00
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
}
void colorfill(float r, float g, float b, float a) {
if(a <= 0) return;
glColor4f(r,g,b,a);
glPushMatrix();
glScalef(SCREEN_W,SCREEN_H,1);
glTranslatef(0.5,0.5,0);
draw_quad();
glPopMatrix();
2011-07-05 15:20:19 +02:00
glColor4f(1,1,1,1);
}
void fade_out(float f) {
colorfill(0, 0, 0, f);
}
2012-08-10 21:27:46 +02:00
double psin(double x) {
2012-07-18 19:19:14 +02:00
return 0.5 + 0.5 * sin(x);
}
2012-08-10 21:27:46 +02:00
double max(double a, double b) {
2012-07-18 19:19:14 +02:00
return (a > b)? a : b;
}
2012-08-10 21:27:46 +02:00
double min(double a, double b) {
2012-07-18 19:19:14 +02:00
return (a < b)? a : b;
}
2012-08-10 21:27:46 +02:00
double clamp(double f, double lower, double upper) {
2012-08-05 03:36:55 +02:00
if(f < lower)
return lower;
if(f > upper)
return upper;
return f;
}
void take_screenshot(void)
{
FILE *out;
char *data;
2011-07-03 19:44:34 +02:00
char outfile[128], *outpath;
time_t rawtime;
struct tm * timeinfo;
int w, h;
w = video.current.width;
h = video.current.height;
data = malloc(3 * w * h);
time(&rawtime);
timeinfo = localtime(&rawtime);
2012-01-14 16:16:07 +01:00
strftime(outfile, 128, "/taisei_%Y%m%d_%H-%M-%S_%Z.png", timeinfo);
2011-07-03 19:44:34 +02:00
outpath = malloc(strlen(outfile) + strlen(get_screenshots_path()) + 1);
strcpy(outpath, get_screenshots_path());
strcat(outpath, outfile);
printf("Saving screenshot as %s\n", outpath);
2012-09-09 19:28:21 +02:00
out = fopen(outpath, "wb");
2011-07-03 19:44:34 +02:00
free(outpath);
if(!out)
{
perror("fopen");
2012-08-10 21:55:12 +02:00
free(data);
return;
}
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
2012-01-14 16:16:07 +01:00
png_structp png_ptr;
png_infop info_ptr;
png_byte **row_pointers;
int y;
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct (png_ptr);
png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_RGB,
2012-01-14 16:16:07 +01:00
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
row_pointers = png_malloc(png_ptr, h*sizeof(png_byte *));
2012-01-14 16:16:07 +01:00
for(y = 0; y < h; y++) {
row_pointers[y] = png_malloc(png_ptr, 8*3*w);
2012-01-14 16:16:07 +01:00
memcpy(row_pointers[y], data + w*3*(h-1-y), w*3);
2012-01-14 16:16:07 +01:00
}
png_init_io(png_ptr, out);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
for(y = 0; y < h; y++)
2012-01-14 16:16:07 +01:00
png_free(png_ptr, row_pointers[y]);
png_free(png_ptr, row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
free(data);
fclose(out);
}
int strendswith(char *s, char *e) {
int ls = strlen(s);
int le = strlen(e);
if(le > ls)
return False;
2012-07-30 22:10:32 +02:00
int i; for(i = 0; i < le; ++i)
if(s[ls-i-1] != e[le-i-1])
return False;
return True;
}
char* difficulty_name(Difficulty diff) {
switch(diff) {
case D_Easy: return "Easy"; break;
2012-07-29 23:01:57 +02:00
case D_Normal: return "Normal"; break;
case D_Hard: return "Hard"; break;
case D_Lunatic: return "Lunatic"; break;
default: return "Unknown"; break;
}
}
void stralloc(char **dest, char *src) {
if(*dest)
free(*dest);
*dest = malloc(strlen(src)+1);
strcpy(*dest, src);
}
// Inputdevice-agnostic method of checking whether a game control is pressed.
// ALWAYS use this instead of SDL_GetKeyState if you need it.
int gamekeypressed(int key) {
2017-02-04 03:56:40 +01:00
return SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(tconfig.intval[key])] || gamepad_gamekeypressed(key);
}