Browse Source

Split up Global and Resources

reduced number of variables.
pull/4/merge
laochailan 10 years ago
parent
commit
0b2aaf0799
  1. BIN
      gfx/part/marilaser_part0.png
  2. 28
      gfx/part/marilaser_part0.svg
  3. 6
      src/CMakeLists.txt
  4. 23
      src/global.c
  5. 21
      src/global.h
  6. 12
      src/main.c
  7. 4
      src/menu/charselect.c
  8. 2
      src/menu/ingamemenu.c
  9. 4
      src/menu/mainmenu.c
  10. 49
      src/player.c
  11. 4
      src/player.h
  12. 11
      src/resource/animation.c
  13. 13
      src/resource/audio.c
  14. 99
      src/resource/resource.c
  15. 49
      src/resource/resource.h
  16. 9
      src/resource/shader.c
  17. 72
      src/resource/texture.c
  18. 13
      src/resource/texture.h
  19. 14
      src/stage.c

BIN
gfx/part/marilaser_part0.png

Before

Width: 14  |  Height: 20  |  Size: 365 B

After

Width: 14  |  Height: 20  |  Size: 469 B

28
gfx/part/marilaser_part0.svg

@ -19,26 +19,16 @@
inkscape:export-ydpi="104.20562"
sodipodi:docname="marilaser_part0.svg">
<defs
id="defs3922">
<filter
inkscape:collect="always"
id="filter4463"
color-interpolation-filters="sRGB">
<feGaussianBlur
inkscape:collect="always"
stdDeviation="0.11800381"
id="feGaussianBlur4465" />
</filter>
</defs>
id="defs3922" />
<sodipodi:namedview
id="base"
pagecolor="#000000"
bordercolor="#666666"
borderopacity="1.0"
borderopacity="1"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:zoom="47.137155"
inkscape:cx="2.3116009"
inkscape:cx="-1.5706869"
inkscape:cy="8.5101465"
inkscape:document-units="px"
inkscape:current-layer="layer1"
@ -70,14 +60,8 @@
id="layer1"
transform="translate(0.41595824,-363.81447)">
<path
inkscape:connector-curvature="0"
style="fill:none;stroke:#ffffff;stroke-width:1.4252522;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
d="m 5.714286,364.9846 5.080582,5.08055 -5.080582,5.08055 -5.08058216,-5.08055 z"
id="path3811" />
<path
inkscape:connector-curvature="0"
style="fill:none;stroke:#ffffff;stroke-width:1.4252522;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
d="m 5.714286,369.57857 5.080582,5.08064 -5.080582,5.08055 -5.08058216,-5.08055 z"
id="path4004" />
id="path4004"
style="fill:none;stroke:#ffffff;stroke-width:1.42525220000000008;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;opacity:0.69924812"
d="m 5.714286,369.57857 5.080582,5.08064 -5.080582,5.08055 -5.08058216,-5.08055 z m 0,-4.59397 5.080582,5.08055 -5.080582,5.08055 -5.08058216,-5.08055 z" />
</g>
</svg>

6
src/CMakeLists.txt

@ -37,12 +37,12 @@ set(SRCs
menu/difficulty.c
menu/charselect.c
stages/stage0.c
resource/resource.c
resource/texture.c
resource/animation.c
resource/font.c
resource/shader.c
resource/audio.c
taisei_err.c
${BISON_cfgparser_OUTPUTS}
${FLEX_cfgscanner_OUTPUTS})
@ -52,6 +52,10 @@ else()
set(SRCs ${SRCs} paths/static.c)
endif()
if(WIN32)
set(SRCs ${SRCs} taisei_err.c)
endif()
add_definitions(-DPREFIX="${CMAKE_INSTALL_PREFIX}")
set(LIBs ${LIBs}

23
src/global.c

@ -10,6 +10,7 @@
#include <time.h>
#include <stdio.h>
#include "paths/native.h"
#include "resource/resource.h"
Global global;
@ -20,13 +21,6 @@ void init_global() {
load_resources();
printf("- fonts:\n");
init_fonts();
if(!tconfig.intval[NO_SHADER]) {
printf("init_fbo():\n");
init_fbo(&global.fbg);
init_fbo(&global.fsec);
printf("-- finished\n");
}
}
void game_over() {
@ -116,22 +110,13 @@ void global_processevent(SDL_Event *event)
int sym = event->key.keysym.sym;
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if(event->type == SDL_KEYDOWN)
{
if(sym == tconfig.intval[KEY_SCREENSHOT])
take_screenshot();
}
keys = SDL_GetKeyState(NULL);
// Catch fullscreen hotkeys (Alt+Enter and the user-defined one)
if((keys[SDLK_LALT] || keys[SDLK_RALT]) && keys[SDLK_RETURN] || keys[tconfig.intval[KEY_FULLSCREEN]])
{
if(!global.fullscreenhotkey_state)
{
if((sym == SDLK_RETURN && (keys[SDLK_LALT] || keys[SDLK_RALT])) || sym == tconfig.intval[KEY_FULLSCREEN])
toggle_fullscreen();
global.fullscreenhotkey_state = 1;
}
}
else global.fullscreenhotkey_state = 0;
}

21
src/global.h

@ -27,6 +27,7 @@
#include "list.h"
#include "config.h"
#include "fbo.h"
#include "resource/resource.h"
#define FILE_PREFIX PREFIX "/share/taisei/"
#define CONFIG_FILE "config"
@ -49,8 +50,6 @@ enum {
PLR_START_BOMBS = 3,
MAX_CONTINUES = 3,
SNDSRC_COUNT = 35,
ACTION_DESTROY,
EVENT_DEATH = -8999,
@ -77,9 +76,6 @@ typedef struct {
typedef struct {
Difficulty diff;
Character plrtype;
ShotMode plrmode;
Player plr;
Projectile *projs;
@ -90,30 +86,18 @@ typedef struct {
Projectile *particles;
int frames;
int lasttime;
int lasttime; // frame limiter
int timer;
Texture *textures;
Animation *animations;
Sound *sounds;
Shader *shaders;
FBO fbg;
FBO fsec;
Boss *boss;
MenuData *menu;
Dialog *dialog;
ALuint sndsrc[SNDSRC_COUNT];
RefArray refs; // for super extra OOP-tardness: references. the cool way.
int game_over;
int points;
int fullscreenhotkey_state;
FPSCounter fps;
} Global;
@ -121,7 +105,6 @@ typedef struct {
extern Global global;
void init_global();
void init_rtt();
void game_over();
void frame_rate();

12
src/main.c

@ -37,17 +37,9 @@ void init_gl() {
glCullFace(GL_BACK);
}
void shutdown() {
delete_textures();
delete_animations();
if(!tconfig.intval[NO_SHADER])
delete_shaders();
if(!tconfig.intval[NO_AUDIO]) {
delete_sounds();
void shutdown() {
if(!tconfig.intval[NO_AUDIO])
alutExit();
}
SDL_FreeSurface(display);
SDL_Quit();

4
src/menu/charselect.c

@ -10,11 +10,11 @@
#include "global.h"
void set_player(void *p) {
global.plrtype = (Character) p;
plr_set_char(&global.plr, (Character) p);
}
void set_shotmode(void *p) {
global.plrmode = (ShotMode) p;
global.plr.shot = (ShotMode) p;
}
void create_char_menu(MenuData *m) {

2
src/menu/ingamemenu.c

@ -46,7 +46,7 @@ void draw_ingame_menu(MenuData *menu) {
glUseProgram(shader);
glUniform1f(glGetUniformLocation(shader, "rad"), rad);
draw_fbo_viewport(&global.fsec);
draw_fbo_viewport(&resources.fsec);
glUseProgram(0);
}

4
src/menu/mainmenu.c

@ -22,6 +22,8 @@ void quit_menu(void *arg) {
void start_story(void *arg) {
MenuData m;
init_player(&global.plr);
troll:
create_difficulty_menu(&m);
if(difficulty_menu_loop(&m) == -1)
@ -31,8 +33,6 @@ troll:
if(char_menu_loop(&m) == -1)
goto troll;
init_player(&global.plr, global.plrtype, global.plrmode);
stage0_loop();
}

49
src/player.c

@ -13,20 +13,21 @@
#include "plrmodes.h"
#include "menu/gameovermenu.h"
void init_player(Player* plr, Character cha, ShotMode shot) {
void init_player(Player* plr) {
memset(plr, 0, sizeof(Player));
plr->pos = VIEWPORT_W/2 + I*(VIEWPORT_H-20);
plr->lifes = PLR_START_LIVES;
plr->bombs = PLR_START_BOMBS;
plr->cha = cha;
plr->shot = shot;
plr->deathtime = -1;
plr->continues = 0;
}
void plr_set_char(Player* plr, Character cha) {
plr->cha = cha;
switch(cha) {
case Youmu:
@ -38,6 +39,25 @@ void init_player(Player* plr, Character cha, ShotMode shot) {
}
}
void plr_set_power(Player *plr, float npow) {
switch(plr->cha) {
case Youmu:
youmu_power(plr, npow);
break;
case Marisa:
marisa_power(plr, npow);
break;
}
plr->power = npow;
if(plr->power > PLR_MAXPOWER)
plr->power = PLR_MAXPOWER;
if(plr->power < 0)
plr->power = 0;
}
void player_draw(Player* plr) {
draw_enemies(plr->slaves);
@ -103,25 +123,6 @@ void player_logic(Player* plr) {
plr_realdeath(plr);
}
void plr_set_power(Player *plr, float npow) {
switch(plr->cha) {
case Youmu:
youmu_power(plr, npow);
break;
case Marisa:
marisa_power(plr, npow);
break;
}
plr->power = npow;
if(plr->power > PLR_MAXPOWER)
plr->power = PLR_MAXPOWER;
if(plr->power < 0)
plr->power = 0;
}
void plr_bomb(Player *plr) {
if(global.frames - plr->recovery >= 0 && plr->bombs > 0) {
Enemy *e;

4
src/player.h

@ -56,11 +56,11 @@ typedef struct {
Animation *ani;
} Player;
void init_player(Player*, Character cha, ShotMode shot);
void init_player(Player*);
void player_draw(Player*);
void player_logic(Player*);
void plr_set_char(Player*, Character);
void plr_set_power(Player *plr, float npow);
void plr_bomb(Player*);

11
src/resource/animation.c

@ -7,13 +7,13 @@
#include "animation.h"
#include "global.h"
#include "resource.h"
#include "list.h"
#include <assert.h>
#include "taisei_err.h"
Animation *init_animation(char *filename) {
Animation *buf = create_element((void **)&global.animations, sizeof(Animation));
Animation *buf = create_element((void **)&resources.animations, sizeof(Animation));
char *beg = strstr(filename, "gfx/") + 4;
char *end = strrchr(filename, '.');
@ -54,7 +54,7 @@ Animation *init_animation(char *filename) {
Animation *get_ani(char *name) {
Animation *a;
Animation *res = NULL;
for(a = global.animations; a; a = a->next) {
for(a = resources.animations; a; a = a->next) {
if(strcmp(a->name, name) == 0) {
res = a;
}
@ -72,7 +72,7 @@ void delete_animation(void **anis, void *ani) {
}
void delete_animations() {
delete_all_elements((void **)&global.animations, delete_animation);
delete_all_elements((void **)&resources.animations, delete_animation);
}
void draw_animation(float x, float y, int row, char *name) {
@ -86,7 +86,8 @@ void draw_animation_p(float x, float y, int row, Animation *ani) {
float s = (float)ani->tex->w/ani->cols/ani->tex->truew;
float t = ((float)ani->tex->h)/ani->tex->trueh/(float)ani->rows;
assert(ani->speed != 0);
if(ani->speed == 0)
errx(-1, "animation speed of %s == 0. relativity?", ani->name);
glPushMatrix();
glTranslatef(x,y,0);

13
src/resource/audio.c

@ -6,6 +6,7 @@
*/
#include "audio.h"
#include "resource.h"
#include "global.h"
#include "list.h"
#include "taisei_err.h"
@ -16,7 +17,7 @@ Sound *load_sound(char *filename) {
errx(-1,"load_sound():\n!- cannot load '%s': %s", filename, alutGetErrorString(alutGetError()));
Sound *snd = create_element((void **)&global.sounds, sizeof(Sound));
Sound *snd = create_element((void **)&resources.sounds, sizeof(Sound));
snd->alsnd = sound;
@ -34,7 +35,7 @@ Sound *load_sound(char *filename) {
Sound *get_snd(char *name) {
Sound *s, *res = NULL;
for(s = global.sounds; s; s = s->next) {
for(s = resources.sounds; s; s = s->next) {
if(strcmp(s->name, name) == 0)
res = s;
}
@ -57,7 +58,7 @@ void play_sound_p(Sound *snd) {
ALuint i,res = -1;
ALint play;
for(i = 0; i < SNDSRC_COUNT; i++) {
alGetSourcei(global.sndsrc[i],AL_SOURCE_STATE,&play);
alGetSourcei(resources.sndsrc[i],AL_SOURCE_STATE,&play);
if(play != AL_PLAYING) {
res = i;
break;
@ -65,8 +66,8 @@ void play_sound_p(Sound *snd) {
}
if(res != -1) {
alSourcei(global.sndsrc[res],AL_BUFFER, snd->alsnd);
alSourcePlay(global.sndsrc[res]);
alSourcei(resources.sndsrc[res],AL_BUFFER, snd->alsnd);
alSourcePlay(resources.sndsrc[res]);
} else {
warnx("play_sound_p():\n!- not enough sources");
}
@ -79,5 +80,5 @@ void delete_sound(void **snds, void *snd) {
}
void delete_sounds() {
delete_all_elements((void **)&global.sounds, delete_sound);
delete_all_elements((void **)&resources.sounds, delete_sound);
}

99
src/resource/resource.c

@ -0,0 +1,99 @@
/*
* 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 "resource.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "paths/native.h"
#include "config.h"
Resources resources;
void recurse_dir(char *path) {
DIR *dir = opendir(path);
if(dir == NULL)
errx(-1, "Can't open directory '%s'", path);
struct dirent *dp;
while((dp = readdir(dir)) != NULL) {
char *buf = malloc(strlen(path) + strlen(dp->d_name)+2);
strcpy(buf, path);
strcat(buf, "/");
strcat(buf, dp->d_name);
struct stat statbuf;
stat(buf, &statbuf);
if(S_ISDIR(statbuf.st_mode) && dp->d_name[0] != '.') {
recurse_dir(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".png") == 0) {
if(strncmp(dp->d_name, "ani_", 4) == 0)
init_animation(buf);
else
load_texture(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".wav") == 0) {
load_sound(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".sha") == 0) {
load_shader(buf);
}
free(buf);
}
}
void load_resources() {
printf("load_resources():\n");
char *path = malloc(strlen(get_prefix())+7);
if(!(resources.state & RS_GfxLoaded)) {
printf("- textures:\n");
strcpy(path, get_prefix());
strcat(path, "gfx");
recurse_dir(path);
resources.state |= RS_GfxLoaded;
}
if(!tconfig.intval[NO_AUDIO] && !(resources.state & RS_SfxLoaded)) {
printf("- sounds:\n");
alGenSources(SNDSRC_COUNT, resources.sndsrc);
strcpy(path, get_prefix());
strcat(path, "sfx");
recurse_dir(path);
resources.state |= RS_SfxLoaded;
}
if(!tconfig.intval[NO_SHADER] && !(resources.state & RS_ShaderLoaded)) {
printf("- shader:\n");
strcpy(path, get_prefix());
strcat(path, "shader");
recurse_dir(path);
printf("init_fbo():\n");
init_fbo(&resources.fbg);
init_fbo(&resources.fsec);
printf("-- finished\n");
resources.state |= RS_ShaderLoaded;
}
}
void free_resources() {
delete_textures();
delete_animations();
if(!tconfig.intval[NO_SHADER])
delete_shaders();
if(!tconfig.intval[NO_AUDIO]) {
delete_sounds();
alutExit();
}
}

49
src/resource/resource.h

@ -0,0 +1,49 @@
/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
*/
#ifndef RESOURCE_H
#define RESOURCE_H
#include "global.h"
#include "texture.h"
#include "animation.h"
#include "audio.h"
#include "shader.h"
#include "font.h"
typedef struct Resources Resources;
typedef enum ResourceState {
RS_GfxLoaded = 1,
RS_SfxLoaded = 2,
RS_ShaderLoaded = 4
} ResourceState;
enum {
SNDSRC_COUNT = 35
};
struct Resources {
ResourceState state;
Texture *textures;
Animation *animations;
Sound *sounds;
Shader *shaders;
ALuint sndsrc[SNDSRC_COUNT];
FBO fbg;
FBO fsec;
};
extern Resources resources;
void load_resources();
#endif

9
src/resource/shader.c

@ -6,7 +6,8 @@
*/
#include "shader.h"
#include "global.h"
#include "resource.h"
#include "config.h"
#include "list.h"
#include <stdio.h>
#include "taisei_err.h"
@ -54,7 +55,7 @@ void load_shader(const char *filename) {
*delim = 0;
char *ftext = delim + DELIM_SIZE;
Shader *sha = create_element((void **)&global.shaders, sizeof(Shader));
Shader *sha = create_element((void **)&resources.shaders, sizeof(Shader));
GLuint vshaderobj;
GLuint fshaderobj;
@ -97,7 +98,7 @@ void load_shader(const char *filename) {
GLuint get_shader(const char *name) {
Shader *s, *res = NULL;
for(s = global.shaders; s; s = s->next) {
for(s = resources.shaders; s; s = s->next) {
if(strcmp(s->name, name) == 0)
res = s;
}
@ -116,5 +117,5 @@ void delete_shader(void **shas, void *sha) {
}
void delete_shaders() {
delete_all_elements((void **)&global.shaders, delete_shader);
delete_all_elements((void **)&resources.shaders, delete_shader);
}

72
src/resource/texture.c

@ -6,79 +6,17 @@
*/
#include "texture.h"
#include "resource.h"
#include "global.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <png.h>
#include "paths/native.h"
#include "taisei_err.h"
#include "audio.h"
#include "shader.h"
#include "list.h"
void recurse_dir(char *path) {
DIR *dir = opendir(path);
if(dir == NULL)
errx(-1, "Couldn't open directory '%s'", path);
struct dirent *dp;
while((dp = readdir(dir)) != NULL) {
char *buf = malloc(strlen(path) + strlen(dp->d_name)+2);
strcpy(buf, path);
strcat(buf, "/");
strcat(buf, dp->d_name);
struct stat statbuf;
stat(buf, &statbuf);
if(S_ISDIR(statbuf.st_mode) && dp->d_name[0] != '.') {
recurse_dir(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".png") == 0) {
if(strncmp(dp->d_name, "ani_", 4) == 0)
init_animation(buf);
else
load_texture(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".wav") == 0) {
load_sound(buf);
} else if(strcmp(dp->d_name + strlen(dp->d_name)-4, ".sha") == 0) {
load_shader(buf);
}
free(buf);
}
}
void load_resources() {
printf("load_resources():\n");
char *path = malloc(strlen(get_prefix())+7);
printf("- textures:\n");
strcpy(path, get_prefix());
strcat(path, "gfx");
recurse_dir(path);
if(!tconfig.intval[NO_AUDIO]) {
printf("- sounds:\n");
alGenSources(SNDSRC_COUNT, global.sndsrc);
strcpy(path, get_prefix());
strcat(path, "sfx");
recurse_dir(path);
}
if(!tconfig.intval[NO_SHADER]) {
printf("- shader:\n");
strcpy(path, get_prefix());
strcat(path, "shader");
recurse_dir(path);
} else {
printf("- shader: disabled.\n");
}
}
#include <png.h>
Texture *get_tex(char *name) {
Texture *t, *res = NULL;
for(t = global.textures; t; t = t->next) {
for(t = resources.textures; t; t = t->next) {
if(strcmp(t->name, name) == 0)
res = t;
}
@ -116,7 +54,7 @@ void delete_texture(void **texs, void *tex) {
}
void delete_textures() {
delete_all_elements((void **)&global.textures, delete_texture);
delete_all_elements((void **)&resources.textures, delete_texture);
}
Texture *load_texture(const char *filename) {
@ -125,7 +63,7 @@ Texture *load_texture(const char *filename) {
if(surface == NULL)
errx(-1,"load_texture():\n!- cannot load '%s'", filename);
Texture *texture = create_element((void **)&global.textures, sizeof(Texture));
Texture *texture = create_element((void **)&resources.textures, sizeof(Texture));
load_sdl_surf(surface, texture);
SDL_FreeSurface(surface);

13
src/resource/texture.h

@ -12,25 +12,22 @@
#include <GL/glew.h>
#include <math.h>
struct Texture;
typedef struct Texture Texture;
typedef struct Texture {
struct Texture *next;
struct Texture *prev;
struct Texture {
Texture *next;
Texture *prev;
int w, h;
int truew, trueh;
char *name;
GLuint gltex;
} Texture;
};
Texture *get_tex(char *name);
Texture *prefix_get_tex(char *name, char *prefix);
void load_textures();
void load_resources();
SDL_Surface *load_png(const char *filename);
void delete_texture(void **texs, void *tex);

14
src/stage.c

@ -140,7 +140,7 @@ void stage_draw() {
if(!tconfig.intval[NO_SHADER]) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
draw_fbo_viewport(&global.fsec);
draw_fbo_viewport(&resources.fsec);
}
} if(global.menu) {
draw_ingame_menu(global.menu);
@ -158,7 +158,7 @@ void stage_draw() {
}
void apply_bg_shaders() {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, global.fsec.fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, resources.fsec.fbo);
if(global.boss) { // Boss background shader
GLuint shader = get_shader("boss_zoom");
@ -168,15 +168,15 @@ void apply_bg_shaders() {
complex pos = fpos + 15*cexp(I*global.frames/5.0);
glUniform2f(glGetUniformLocation(shader, "blur_orig"),
creal(pos)/global.fbg.nw, cimag(pos)/global.fbg.nh);
creal(pos)/resources.fbg.nw, cimag(pos)/resources.fbg.nh);
glUniform2f(glGetUniformLocation(shader, "fix_orig"),
creal(fpos)/global.fbg.nw, cimag(fpos)/global.fbg.nh);
creal(fpos)/resources.fbg.nw, cimag(fpos)/resources.fbg.nh);
glUniform1f(glGetUniformLocation(shader, "blur_rad"), 0.2+0.05*sin(global.frames/10.0));
glUniform1f(glGetUniformLocation(shader, "rad"), 0.3);
glUniform1f(glGetUniformLocation(shader, "ratio"), (float)global.fbg.nh/global.fbg.nw);
glUniform1f(glGetUniformLocation(shader, "ratio"), (float)resources.fbg.nh/resources.fbg.nw);
}
draw_fbo_viewport(&global.fbg);
draw_fbo_viewport(&resources.fbg);
glUseProgramObjectARB(0);
@ -250,7 +250,7 @@ void stage_loop(StageRule start, StageRule end, StageRule draw, StageRule event)
stage_logic();
if(!tconfig.intval[NO_SHADER])
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, global.fbg.fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, resources.fbg.fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
stage_draw();

Loading…
Cancel
Save