dynamic texture loader

The static texture loading was replaced by a dynamic loader. Every png file in gfx/ is (recursively) loaded at the beginning and accessed via get_tex(name) or get_ani(name).name for "gfx/stage1/border.png" would be "stage1/border".

Sprite information is to be specified in the filename in the pattern: ani_${row count}_${col count}_${animation frequency}_${name}.png
This commit is contained in:
laochailan 2011-03-19 16:21:48 +01:00
parent 58e0c9c3c2
commit ec85251571
30 changed files with 208 additions and 421 deletions

View file

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 609 B

After

Width:  |  Height:  |  Size: 609 B

View file

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

View file

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View file

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View file

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

Before

Width:  |  Height:  |  Size: 853 B

After

Width:  |  Height:  |  Size: 853 B

View file

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

Before

Width:  |  Height:  |  Size: 820 B

After

Width:  |  Height:  |  Size: 820 B

View file

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

View file

@ -10,23 +10,74 @@
#include <assert.h>
void init_animation(Animation *buf, int rows, int cols, int speed, const char *filename) {
buf->rows = rows;
buf->cols = cols;
Animation *init_animation(char *filename) {
Animation *buf = create_element((void **)&global.animations, sizeof(Animation));
buf->speed = speed;
char *beg = strstr(filename, "gfx/") + 4;
char *end = strrchr(filename, '.');
char *name = malloc(end - beg + 1);
memset(name, 0, end - beg + 1);
strncpy(name, beg, end-beg);
load_texture(filename,&buf->tex);
buf->w = buf->tex.w/cols;
buf->h = buf->tex.h/rows;
char* tok;
strtok(name, "_");
if((tok = strtok(NULL, "_")) == NULL)
errx(-1, "init_animation():\n!- bad 'rows' in filename '%s'", name);
buf->rows = atoi(tok);
if((tok = strtok(NULL, "_")) == NULL)
errx(-1, "init_animation():\n!- bad 'cols' in filename '%s'", name);
buf->cols = atoi(tok);
if((tok = strtok(NULL, "_")) == NULL)
errx(-1, "init_animation():\n!- bad 'speed' in filename '%s'", name);
buf->speed = atoi(tok);
if((tok = strtok(NULL, "_")) == NULL)
errx(-1, "init_animation():\n!- bad 'name' in filename '%s'", name);
buf->name = malloc(strlen(tok)+1);
memset(buf->name, 0, strlen(tok)+1);
strcpy(buf->name, tok);
buf->tex = load_texture(filename);
buf->w = buf->tex->w/buf->cols;
buf->h = buf->tex->h/buf->rows;
printf("-- initialized animation '%s'\n", buf->name);
return buf;
}
void draw_animation(int x, int y, int row, const Animation *ani) { // matrices are cool
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ani->tex.gltex);
Animation *get_ani(char *name) {
Animation *a;
Animation *res = NULL;
for(a = global.animations; a; a = a->next) {
if(strcmp(a->name, name) == 0) {
res = a;
}
}
float s = (float)ani->tex.w/ani->cols/ani->tex.truew;
float t = ((float)ani->tex.h)/ani->tex.trueh/(float)ani->rows;
if(res == NULL)
errx(-1,"get_ani():\n!- cannot load animation '%s'", name);
return res;
}
void delete_animations() {
delete_all_elements((void **)&global.animations);
}
void draw_animation(float x, float y, int row, char *name) {
draw_animation_p(x, y, row, get_ani(name));
}
void draw_animation_p(float x, float y, int row, Animation *ani) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ani->tex->gltex);
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);

View file

@ -10,7 +10,12 @@
#include "texture.h"
typedef struct {
struct Animation;
typedef struct Animation {
struct Animation *next;
struct Animation *prev;
int rows;
int cols;
@ -18,10 +23,14 @@ typedef struct {
int speed;
Texture tex;
char *name;
Texture *tex;
} Animation;
void init_animation(Animation *buf, int rows, int cols, int speed, const char *filename);
void draw_animation(int x, int y, int row, const Animation *ani);
Animation *init_animation(char *filename);
Animation *get_ani(char *name);
void delete_animations();
void draw_animation(float x, float y, int row, char *name);
void draw_animation_p(float x, float y, int row, Animation *ani);
#endif

View file

@ -20,7 +20,7 @@ void create_fairy(complex pos, int hp, FairyRule rule, complex args, ...) {
f->hp = hp;
f->dir = 0;
f->ani = &global.textures.fairy;
f->ani = get_ani("fairy");
va_list ap;
int i;
@ -41,7 +41,7 @@ void delete_fairy(Fairy *fairy) {
void draw_fairies() {
glEnable(GL_TEXTURE_2D);
Texture *tex = &global.textures.fairy_circle;
Texture *tex = get_tex("fairy_circle");
glBindTexture(GL_TEXTURE_2D, tex->gltex);
Fairy *f;
@ -81,7 +81,7 @@ void draw_fairies() {
glCullFace(GL_FRONT);
glScalef(-1,1,1);
}
draw_animation(0, 0, f->moving, f->ani);
draw_animation_p(0, 0, f->moving, f->ani);
glCullFace(GL_BACK);
glPopMatrix();

View file

@ -30,6 +30,6 @@ Texture *load_text(const char *text, TTF_Font *font) {
void draw_text(const char *text, int x, int y, TTF_Font *font) {
Texture *tex = load_text(text, font);
draw_texture(x, y, tex);
draw_texture_p(x, y, tex);
free_texture(tex);
}

View file

@ -11,29 +11,14 @@
Global global;
void init_textures() { // TODO: dynamic loader.
load_texture(FILE_PREFIX "gfx/wasser.png", &global.textures.water);
load_texture(FILE_PREFIX "gfx/stage1/border.png", &global.textures.border);
load_texture(FILE_PREFIX "gfx/hud.png", &global.textures.hud);
load_texture(FILE_PREFIX "gfx/fairy_circle.png", &global.textures.fairy_circle);
load_texture(FILE_PREFIX "gfx/focus.png", &global.textures.focus);
load_texture(FILE_PREFIX "gfx/power.png", &global.textures.poweritems);
init_animation(&global.textures.fairy, 2, 2, 15, FILE_PREFIX "gfx/fairy.png");
}
void init_global() {
init_player(&global.plr, Youmu);
init_textures();
load_projectiles();
init_fonts();
global.projs = NULL;
global.fairies = NULL;
global.poweritems = NULL;
global.textures = NULL;
global.animations = NULL;
global.frames = 0;
global.game_over = 0;
@ -41,6 +26,9 @@ void init_global() {
global.fps = 0;
global.lasttime = 0;
load_textures();
init_fonts();
}
void game_over() {

View file

@ -41,18 +41,8 @@ typedef struct {
int frames;
struct {
Texture hud;
Texture projwave;
Texture water;
Texture border;
Texture fairy_circle;
Texture focus;
Texture poweritems;
Animation fairy;
} textures;
Texture *textures;
Animation *animations;
int game_over;

View file

@ -38,6 +38,9 @@ void init_gl() {
}
void shutdown() {
delete_textures();
delete_animations();
SDL_FreeSurface(display);
SDL_Quit();
}

View file

@ -22,7 +22,7 @@ void init_player(Player* plr, Character cha) {
plr->cha = cha;
init_animation(&plr->ani, 2, 4, 5, FILE_PREFIX "gfx/youmu.png");
plr->ani = get_ani("youmu");
}
void player_draw(Player* plr) {
@ -34,7 +34,7 @@ void player_draw(Player* plr) {
glRotatef(global.frames*10, 0, 0, 1);
glScalef(1, 1, 1);
glColor4f(1,1,1,0.2);
draw_texture(0, 0, &global.textures.fairy_circle);
draw_texture(0, 0, "fairy_circle");
glColor4f(1,1,1,1);
glPopMatrix();
}
@ -45,7 +45,7 @@ void player_draw(Player* plr) {
glScalef(-1,1,1);
}
draw_animation(0, 0, !plr->moving, &plr->ani);
draw_animation_p(0, 0, !plr->moving, plr->ani);
if(plr->dir)
glPopMatrix();
@ -56,7 +56,7 @@ void player_draw(Player* plr) {
glPushMatrix();
glColor4f(1,1,1,fabs((float)plr->focus/30.0f));
glRotatef(global.frames, 0, 0, -1);
draw_texture(0, 0, &global.textures.focus);
draw_texture(0, 0, "focus");
glColor4f(1,1,1,1);
glPopMatrix();
}
@ -66,14 +66,14 @@ void player_draw(Player* plr) {
void player_logic(Player* plr) {
if(plr->fire && !(global.frames % 4)) {
create_projectile(&_projs.youmu, plr->pos + 10 - I*20, ((Color){1,1,1}), linear, -20I)->type = PlrProj;
create_projectile(&_projs.youmu, plr->pos - 10 - I*20, ((Color){1,1,1}), linear, -20I)->type = PlrProj;
create_projectile("youmu", plr->pos + 10 - I*20, ((Color){1,1,1}), linear, -20I)->type = PlrProj;
create_projectile("youmu", plr->pos - 10 - I*20, ((Color){1,1,1}), linear, -20I)->type = PlrProj;
if(plr->power >= 2) {
float a = 0.20;
if(plr->focus > 0) a = 0.06;
create_projectile(&_projs.youmu, plr->pos + 10 - I*20, ((Color){1,1,1}), linear, I*-20*cexp(-I*a))->type = PlrProj;
create_projectile(&_projs.youmu, plr->pos - 10 - I*20, ((Color){1,1,1}), linear, I*-20*cexp(I*a))->type = PlrProj;
create_projectile("youmu", plr->pos + 10 - I*20, ((Color){1,1,1}), linear, I*-20*cexp(-I*a))->type = PlrProj;
create_projectile("youmu", plr->pos - 10 - I*20, ((Color){1,1,1}), linear, I*-20*cexp(I*a))->type = PlrProj;
}
}

View file

@ -32,7 +32,7 @@ typedef struct {
Character cha;
Animation ani;
Animation *ani;
} Player;
void init_player(Player*, Character cha);

View file

@ -26,8 +26,9 @@ void delete_poweritem(Poweritem *poweritem) {
void draw_poweritems() {
Poweritem *p;
Texture *tex = get_tex("poweritem");
for(p = global.poweritems; p; p = p->next)
draw_texture(creal(p->pos), cimag(p->pos), &global.textures.poweritems);
draw_texture_p(creal(p->pos), cimag(p->pos), tex);
}
void free_poweritems() {

View file

@ -12,25 +12,20 @@
#include "global.h"
#include "list.h"
ProjCache _projs;
void load_projectiles() {
load_texture(FILE_PREFIX "gfx/projectiles/ball.png", &_projs.ball);
load_texture(FILE_PREFIX "gfx/projectiles/rice.png", &_projs.rice);
load_texture(FILE_PREFIX "gfx/projectiles/bigball.png", &_projs.bigball);
load_texture(FILE_PREFIX "gfx/proyoumu.png", &_projs.youmu);
}
Projectile *create_projectile(Texture *tex, complex pos, Color clr,
Projectile *create_projectile(char *name, complex pos, Color clr,
ProjRule rule, complex args, ...) {
Projectile *p = create_element((void **)&global.projs, sizeof(Projectile));
char buf[128];
strcpy(buf, "proj/");
strcat(buf, name);
p->birthtime = global.frames;
p->pos = pos;
p->pos0 = pos;
p->angle = 0;
p->rule = rule;
p->tex = tex;
p->tex = get_tex(buf);
p->type = FairyProj;
p->clr = clr;

View file

@ -43,19 +43,9 @@ typedef struct Projectile {
complex args[4];
} Projectile;
typedef struct {
Texture ball;
Texture rice;
Texture bigball;
Texture youmu;
} ProjCache;
extern ProjCache _projs;
void load_projectiles();
Projectile *create_projectile(Texture *tex, complex pos, Color clr, ProjRule rule, complex args, ...);
Projectile *create_projectile(char *name, complex pos, Color clr, ProjRule rule, complex args, ...);
void delete_projectile(Projectile *proj);
void draw_projectile(Projectile *proj);
void draw_projectiles();

View file

@ -63,13 +63,13 @@ void stage_input() {
global.plr.dir = 0;
}
if(keys[SDLK_LEFT] && creal(global.plr.pos) - global.plr.ani.w/2 - speed > 0)
if(keys[SDLK_LEFT] && creal(global.plr.pos) - global.plr.ani->w/2 - speed > 0)
global.plr.pos -= speed;
if(keys[SDLK_RIGHT] && creal(global.plr.pos) + global.plr.ani.w/2 + speed < VIEWPORT_W)
if(keys[SDLK_RIGHT] && creal(global.plr.pos) + global.plr.ani->w/2 + speed < VIEWPORT_W)
global.plr.pos += speed;
if(keys[SDLK_UP] && cimag(global.plr.pos) - global.plr.ani.h/2 - speed > 0)
if(keys[SDLK_UP] && cimag(global.plr.pos) - global.plr.ani->h/2 - speed > 0)
global.plr.pos -= I*speed;
if(keys[SDLK_DOWN] && cimag(global.plr.pos) + global.plr.ani.h/2 + speed < VIEWPORT_H)
if(keys[SDLK_DOWN] && cimag(global.plr.pos) + global.plr.ani->h/2 + speed < VIEWPORT_H)
global.plr.pos += I*speed;
}
@ -88,7 +88,7 @@ void stage_draw() {
char buf[16];
sprintf(buf, "Power: %.2f", global.plr.power);
draw_texture(SCREEN_W/2, SCREEN_H/2, &global.textures.hud);
draw_texture(SCREEN_W/2, SCREEN_H/2, "hud");
draw_text(buf, SCREEN_W-200, 200, _fonts.biolinum);
}

View file

@ -12,9 +12,9 @@
#include "../global.h"
void simpleFairy(Fairy *f) {
if(!((global.frames - f->birthtime) % 50)) {
create_projectile(&_projs.rice, f->pos, ((Color){0,0,1}), linear,3+ 2*I);
}
if(!((global.frames - f->birthtime) % 50))
create_projectile("rice", f->pos, ((Color){0,0,1}), linear,3+ 2*I);
f->moving = 1;
f->dir = creal(f->args[0]) < 0;
@ -37,12 +37,14 @@ void stage0_draw() {
glTranslatef(0,VIEWPORT_H,0);
glRotatef(110, 1, 0, 0);
Texture *water = get_tex("wasser");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, global.textures.water.gltex);
glBindTexture(GL_TEXTURE_2D, water->gltex);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0,global.frames/(float)global.textures.water.h, 0);
glTranslatef(0,global.frames/(float)water->h, 0);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS);
@ -58,7 +60,7 @@ void stage0_draw() {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, global.textures.border.gltex);
glBindTexture(GL_TEXTURE_2D, get_tex("stage1/border")->gltex);
// glColor3f(1,0.75,0.75);
glBegin(GL_QUADS);
@ -88,11 +90,11 @@ void stage0_events() {
create_fairy(0 + I*100, 3, simpleFairy, 2);
// create_fairy(VIEWPORT_W-1, 10, -1, 180, 3, simpleFairy);
// create_fairy(VIEWPORT_W-1, 200, -1, 180, 3, simpleFairy);
create_projectile(&_projs.ball, VIEWPORT_W/2, ((Color) {0,0,1}), linear, 2*I);
create_projectile("ball", VIEWPORT_W/2, ((Color) {0,0,1}), linear, 2*I);
}
}
void stage0_loop() {
void stage0_loop() {
stage_start();
glEnable(GL_FOG);
GLfloat clr[] = { 0.1, 0.1, 0.1, 0 };

View file

@ -6,15 +6,77 @@
*/
#include "texture.h"
#include "global.h"
#include <dirent.h>
void load_texture(const char *filename, Texture* texture) {
void recurse_dir(char *path) {
DIR *dir = opendir(path);
struct dirent *dp;
char buf[512];
while((dp = readdir(dir)) != NULL) {
strncpy(buf, path, sizeof(buf));
strncat(buf, "/", sizeof(buf));
strncat(buf, dp->d_name, sizeof(buf));
if(dp->d_type == DT_DIR && 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);
}
}
}
void load_textures() {
printf("load_textures():\n");
char *path = malloc(sizeof(FILE_PREFIX)+4);
strcpy(path, FILE_PREFIX);
strncat(path, "gfx", sizeof(FILE_PREFIX)+4);
recurse_dir(path);
}
Texture *get_tex(char *name) {
Texture *t, *res = NULL;
for(t = global.textures; t; t = t->next) {
if(strcmp(t->name, name) == 0)
res = t;
}
if(res == NULL)
errx(-1,"get_tex():\n!- cannot load texture '%s'", name);
return res;
}
void delete_textures() {
delete_all_elements((void **)&global.textures);
}
Texture *load_texture(const char *filename) {
SDL_Surface *surface = IMG_Load(filename);
if(surface == NULL)
err(EXIT_FAILURE,"load_texture():\n-- cannot load '%s'", filename);
err(-1,"load_texture():\n!- cannot load '%s'", filename);
Texture *texture = create_element((void **)&global.textures, sizeof(Texture));
load_sdl_surf(surface, texture);
SDL_FreeSurface(surface);
char *beg = strstr(filename, "gfx/") + 4;
char *end = strrchr(filename, '.');
texture->name = malloc(end - beg + 1);
memset(texture->name, 0, end-beg + 1);
strncpy(texture->name, beg, end-beg);
printf("-- loaded '%s' as '%s'\n", filename, texture->name);
return texture;
}
void load_sdl_surf(SDL_Surface *surface, Texture *texture) {
@ -62,7 +124,11 @@ void free_texture(Texture *tex) {
free(tex);
}
void draw_texture(int x, int y, Texture *tex) {
void draw_texture(int x, int y, char *name) {
draw_texture_p(x, y, get_tex(name));
}
void draw_texture_p(int x, int y, Texture *tex) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex->gltex);

View file

@ -13,15 +13,27 @@
#include <SDL/SDL_opengl.h>
#include <math.h>
typedef struct {
struct Texture;
typedef struct Texture {
struct Texture *next;
struct Texture *prev;
int w, h;
int truew, trueh;
char *name;
GLuint gltex;
} Texture;
void load_texture(const char *filename, Texture *texture);
Texture *get_tex(char *name);
void load_textures();
void delete_textures();
Texture *load_texture(const char *filename);
void load_sdl_surf(SDL_Surface *surface, Texture *texture);
void free_texture(Texture *tex);
void draw_texture(int x, int y, Texture *tex);
void draw_texture(int x, int y, char *name);
void draw_texture_p(int x, int y, Texture *tex);
#endif

View file

@ -1,320 +0,0 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 62fbe80..723e0ac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,3 +6,11 @@ add_subdirectory(src)
set(DATA_DIR "share/taisei")
install(DIRECTORY gfx DESTINATION ${DATA_DIR})
+# uninstall target
+configure_file(
+ "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
+ "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
+ IMMEDIATE @ONLY)
+
+add_custom_target(uninstall
+ COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
\ No newline at end of file
diff --git a/cmake_uninstall.cmake.in b/cmake_uninstall.cmake.in
new file mode 100644
index 0000000..97b93af
--- /dev/null
+++ b/cmake_uninstall.cmake.in
@@ -0,0 +1,21 @@
+if (NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+ message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
+endif(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+
+file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
+string(REGEX REPLACE "\n" ";" files "${files}")
+foreach (file ${files})
+ message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
+ if (EXISTS "$ENV{DESTDIR}${file}")
+ execute_process(
+ COMMAND @CMAKE_COMMAND@ -E remove "$ENV{DESTDIR}${file}"
+ OUTPUT_VARIABLE rm_out
+ RESULT_VARIABLE rm_retval
+ )
+ if(NOT ${rm_retval} EQUAL 0)
+ message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
+ endif (NOT ${rm_retval} EQUAL 0)
+ else (EXISTS "$ENV{DESTDIR}${file}")
+ message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
+ endif (EXISTS "$ENV{DESTDIR}${file}")
+endforeach(file)
\ No newline at end of file
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 910f5a4..1862cab 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -7,6 +7,7 @@ set(SRCs
projectile.c
animation.c
fairy.c
+ poweritem.c
list.c
font.c
stages/stage0.c)
diff --git a/src/fairy.c b/src/fairy.c
index d14d8ca..b98c51e 100644
--- a/src/fairy.c
+++ b/src/fairy.c
@@ -28,6 +28,7 @@ void create_fairy(int x, int y, int v, int angle, int hp, FairyRule rule) {
}
void delete_fairy(Fairy *fairy) {
+ if(global.plr.power < 6) create_poweritem(fairy->x, fairy->y,0.03,0,simpleItem);
delete_element((void **)&global.fairies, fairy);
}
diff --git a/src/fairy.h b/src/fairy.h
index 7f90755..221199c 100644
--- a/src/fairy.h
+++ b/src/fairy.h
@@ -17,7 +17,6 @@ typedef void (*FairyRule)(struct Fairy*);
typedef struct Fairy {
struct Fairy *next;
struct Fairy *prev;
-
long birthtime;
char hp;
diff --git a/src/global.c b/src/global.c
index 69d29b8..c05af37 100644
--- a/src/global.c
+++ b/src/global.c
@@ -17,6 +17,7 @@ void init_textures() {
load_texture(FILE_PREFIX "gfx/hud.png", &global.textures.hud);
load_texture(FILE_PREFIX "gfx/fairy_circle.png", &global.textures.fairy_circle);
load_texture(FILE_PREFIX "gfx/focus.png", &global.textures.focus);
+ load_texture(FILE_PREFIX "gfx/item_power.png", &global.textures.poweritems);
init_animation(&global.textures.fairy, 2, 2, 15, FILE_PREFIX "gfx/fairy.png");
}
@@ -31,6 +32,7 @@ void init_global() {
global.projs = NULL;
global.fairies = NULL;
+ global.poweritems = NULL;
global.frames = 0;
global.game_over = 0;
diff --git a/src/global.h b/src/global.h
index 559ef2f..a477d0a 100644
--- a/src/global.h
+++ b/src/global.h
@@ -1,6 +1,6 @@
/*
* This software is licensed under the terms of the MIT-License
- * See COPYING for further information.
+ * See COPYING for further information.
* ---
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
*/
@@ -8,54 +8,58 @@
#ifndef GLOBAL_H
#define GLOBAL_H
+#include <SDL/SDL_opengl.h>
#include <SDL/SDL.h>
#include "player.h"
#include "projectile.h"
#include "fairy.h"
+#include "poweritem.h"
enum {
RESX = 800,
RESY = 600,
-
+
SCREEN_W = 800,
SCREEN_H = 600,
-
+
VIEWPORT_X = 40,
VIEWPORT_Y = 20,
VIEWPORT_W = 480,
VIEWPORT_H = 560,
-
+
FPS = 60
};
#define FILE_PREFIX PREFIX "/share/taisei/"
typedef struct {
- Player plr;
+ Player plr;
Projectile *projs;
Fairy *fairies;
-
+ Poweritem *poweritems;
+
int frames;
-
+
struct {
Texture hud;
-
+
Texture projwave;
Texture water;
Texture border;
Texture fairy_circle;
Texture focus;
-
+ Texture poweritems;
+
Animation fairy;
} textures;
-
+
int game_over;
-
+
int time;
-
+
int fpstime; // frame counter
int fps;
-
+
int lasttime;
} Global;
diff --git a/src/poweritem.c b/src/poweritem.c
new file mode 100644
index 0000000..7e1775e
--- /dev/null
+++ b/src/poweritem.c
@@ -0,0 +1,80 @@
+/*
+ * This software is licensed under the terms of the MIT-License
+ * See COPYING for further information.
+ * ---
+ * Copyright (C) 2011, Lukas Weber <laochailan@web.de>
+ * Code by Juergen Kieslich
+ */
+
+#include "poweritem.h"
+#include "global.h"
+#include "list.h"
+
+void create_poweritem(int x, int y, float acc, int angle, ItemRule rule) {
+ Poweritem *p = create_element((void **)&global.poweritems, sizeof(Poweritem));
+ p->x = x;
+ p->y = y;
+ p->sy = y;
+ p->acc = acc;
+ p->velo = -3;
+ p->angle = angle;
+ p->birthtime = global.frames;
+ p->tex = &global.textures.poweritems;
+ p->rule = rule;
+}
+
+
+void delete_poweritem(Poweritem *poweritem) {
+ delete_element((void **)&global.poweritems, poweritem);
+}
+
+void draw_poweritems() {
+ Poweritem *p;
+ Texture *tex = &global.textures.poweritems;
+
+ for(p = global.poweritems; p; p = p->next){
+ draw_texture(p->x, p->y, &global.textures.poweritems);
+ }
+}
+
+void free_poweritems() {
+ delete_all_elements((void **)&global.poweritems);
+}
+
+void simpleItem(int *y,int sy, float acc, long t, float *velo) {
+ if(*velo < 3 ) *velo += acc;
+ *y = sy + (*velo * t);
+}
+
+void process_poweritems() {
+ Poweritem *poweritem = global.poweritems, *del = NULL;
+ int v;
+ while(poweritem != NULL) {
+ poweritem->rule(&poweritem->y, poweritem->sy, poweritem->acc, global.frames - poweritem->birthtime, &poweritem->velo);
+ v = collision(poweritem);
+ if(v == 1) {
+ global.plr.power += 0.1;
+ }
+ if(v == 1 || poweritem->x < -20 || poweritem->x > VIEWPORT_W + 20 || poweritem->y < -20 || poweritem->y > VIEWPORT_H + 20) {
+ del = poweritem;
+ poweritem = poweritem->next;
+ delete_poweritem(del);
+ } else {
+ poweritem = poweritem->next;
+ }
+ }
+}
+
+int collision(Poweritem *p) {
+ float angle = atan((float)(global.plr.y - p->y)/(global.plr.x - p->x));
+
+ int projr = sqrt(pow(p->tex->w/4*cos(angle),2)*8/10 + pow(p->tex->h/2*sin(angle)*8/10,2));
+ if(sqrt(pow(p->x-global.plr.x,2) + pow(p->y-global.plr.y,2)) < projr+1) {
+ // most magic line in the game.
+ // i tried to get some touhou feel.
+ // +/- 9 didn't really work so i used +1
+ return 1;
+ return 0;
+
+ }
+}
diff --git a/src/poweritem.h b/src/poweritem.h
new file mode 100644
index 0000000..a7b01c1
--- /dev/null
+++ b/src/poweritem.h
@@ -0,0 +1,40 @@
+/*
+ * This software is licensed under the terms of the MIT-License
+ * See COPYING for further information.
+ * ---
+ * Copyright (C) 2011, Lukas Weber <laochailan@web.de>
+ * Code by: Juergen Kieslich
+ */
+
+#ifndef POWERITEM_H
+#define POWERITEM_H
+
+#include "texture.h"
+
+struct Poweritem;
+typedef void (*ItemRule)(int *y,int sy, float acc, long time, float *velo);
+
+typedef struct Poweritem{
+ struct Poweritem *next;
+ struct Poweritem *prev;
+
+ long birthtime;
+ int x, y, sy;
+ char angle;
+ float acc;
+ float velo;
+ Texture *tex;
+
+ ItemRule rule;
+} Poweritem;
+
+void create_poweritem(int x, int y, float acc, int angle, ItemRule rule);
+void delete_poweritem(Poweritem *poweritem);
+void draw_poweritems();
+void free_poweritems();
+
+int collision(Poweritem *p);
+void process_poweritems();
+
+void simpleItem(int *y, int sy, float acc, long time, float *velo);
+#endif
diff --git a/src/projectile.c b/src/projectile.c
index 0b2226d..bba6df7 100644
--- a/src/projectile.c
+++ b/src/projectil