summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/Makefile5
-rwxr-xr-xsrc/image.c89
-rwxr-xr-xsrc/modules.c1
-rw-r--r--src/music.c102
-rw-r--r--src/sound.c56
-rw-r--r--src/storage.c136
6 files changed, 222 insertions, 167 deletions
diff --git a/src/Makefile b/src/Makefile
index 2b66261..aa1bad1 100755
--- a/src/Makefile
+++ b/src/Makefile
@@ -9,7 +9,7 @@ CFLAGS = -g -O0 -std=c99 -I../include -Wall `sdl-config --cflags`
BUILD_DIR = .
LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net
-FILES = interface.o idManager.o font.o list.o modules.o image.o layer.o director.o\
+FILES = interface.o idManager.o storage.o font.o list.o modules.o image.o layer.o director.o\
configFile.o tux.o main.o checkFront.o\
screen.o screen_world.o shot.o myTimer.o panel.o net_multiplayer.o \
buffer.o arena.o arenaFile.o textFile.o audio.o sound.o music.o gun.o \
@@ -27,6 +27,9 @@ main.o: main.c
interface.o: interface.c ../include/interface.h
$(CC) $(CFLAGS) -o $(BUILD_DIR)/interface.o -c interface.c
+storage.o: storage.c ../include/storage.h
+ $(CC) $(CFLAGS) -o $(BUILD_DIR)/storage.o -c storage.c
+
modules.o: modules.c ../include/modules.h
$(CC) $(CFLAGS) -o $(BUILD_DIR)/modules.o -c modules.c
diff --git a/src/image.c b/src/image.c
index 1462b56..733d480 100755
--- a/src/image.c
+++ b/src/image.c
@@ -8,9 +8,10 @@
#include "image.h"
#include "list.h"
#include "path.h"
+#include "storage.h"
#include "string_length.h"
-static list_t *listImageData;
+static list_t *listStorage;
static bool_t isImageDataInit = FALSE;
@@ -27,7 +28,7 @@ void initImageData()
assert( isInterfaceInicialized() == TRUE );
printf("init image database..\n");
- listImageData = newList();
+ listStorage = newStorage();
isImageDataInit = TRUE;
}
@@ -60,6 +61,12 @@ static SDL_Surface *loadImage(const char *filename, int alpha)
return ret;
}
+static void destroySDLSurface(void *p)
+{
+ assert( p != NULL );
+ SDL_FreeSurface((SDL_Surface *)p);
+}
+
/*
* Prida obrazok do globalneho zoznamu obrazkov
* *file - nazov suboru v ktorom je obrazok ulozeny
@@ -68,33 +75,19 @@ static SDL_Surface *loadImage(const char *filename, int alpha)
*/
SDL_Surface* addImageData(char *file, int alpha, char *name, char *group)
{
- image_data_t *new;
+ SDL_Surface *new;
assert( file != NULL );
assert( name != NULL );
assert( group != NULL );
- new = malloc(sizeof(image_data_t));
+ new = loadImage(file, alpha);
- new->image = loadImage(file, alpha);
- //if(strstr(file,"BMP")!=NULL)SDL_SetColorKey(new->image, SDL_SRCCOLORKEY, SDL_MapRGB(new->image->format, 255, 255, 255));
- new->name = strdup(name);
- new->group = strdup(group);
-
- addList(listImageData, new);
+ addItemToStorage(listStorage, group, name, new );
printf("load image %s\n", file);
- return new->image;
-}
-
-static void destroyImageData(image_data_t *p)
-{
- assert( p != NULL );
- free(p->name);
- free(p->group);
- SDL_FreeSurface(p->image);
- free(p);
+ return new;
}
/*
@@ -103,74 +96,36 @@ static void destroyImageData(image_data_t *p)
*/
SDL_Surface* getImage(char *group, char *name)
{
- image_data_t *this;
-
assert( group != NULL );
assert( name != NULL );
- int i;
-
- for(i = 0 ; i < listImageData->count; i++)
- {
- this = (image_data_t *) listImageData->list[i];
-
- if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 )
- {
- return this->image;
- }
- }
-
- return NULL;
+ return getItemFromStorage(listStorage, group, name);
}
void delImage(char *group, char *name)
{
- image_data_t *this;
- int i;
-
assert( group != NULL );
assert( name != NULL );
- for(i = 0 ; i < listImageData->count; i++)
- {
- this = (image_data_t *) listImageData->list[i];
-
- if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 )
- {
- delListItem(listImageData, i, destroyImageData);
- break;
- }
-
- }
+ delItemFromStorage(listStorage, group, name, destroySDLSurface);
}
void delAllImageInGroup(char *group)
{
- image_data_t *this;
- int i;
-
assert( group != NULL );
- printf("del all image in group %s\n", group);
-
- for(i = 0 ; i < listImageData->count; i++)
- {
- this = (image_data_t *) listImageData->list[i];
-
- if( strcmp(group, this->group) == 0 )
- {
- delListItem(listImageData, i, destroyImageData);
- i--;
- }
- }
+ delAllItemFromStorage(listStorage, group, destroySDLSurface);
}
void drawImage(SDL_Surface *p, int x,int y, int px, int py, int w, int h)
{
+ static SDL_Surface *screen = NULL;
SDL_Rect dst_rect, src_rect;
- SDL_Surface *screen;
- screen = getSDL_Screen();
+ if( screen == NULL )
+ {
+ screen = getSDL_Screen();
+ }
dst_rect.x = x;
dst_rect.y = y;
@@ -189,7 +144,7 @@ void drawImage(SDL_Surface *p, int x,int y, int px, int py, int w, int h)
void quitImageData()
{
printf("quit image database..\n");
- destroyListItem(listImageData, destroyImageData);
+ destroyStorage(listStorage, destroySDLSurface);
isImageDataInit = FALSE;
}
diff --git a/src/modules.c b/src/modules.c
index b5c87f0..6ba3234 100755
--- a/src/modules.c
+++ b/src/modules.c
@@ -125,7 +125,6 @@ static int destroyModule(module_t *p)
dlclose(p->image);
free(p);
-
printf("destroy module..\n");
return 0;
diff --git a/src/music.c b/src/music.c
index 2fd8e31..e4fb812 100644
--- a/src/music.c
+++ b/src/music.c
@@ -10,12 +10,13 @@
#include "string_length.h"
#include "audio.h"
#include "music.h"
+#include "storage.h"
-static list_t *listMusic;
+static list_t *listStorage;
static bool_t isMusicInit = FALSE;
static bool_t var_isMusicActive = TRUE;
-static music_t *currentMusic;
+static Mix_Music *currentMusic;
bool_t isMusicInicialized()
{
@@ -30,7 +31,7 @@ void initMusic()
return;
}
- listMusic = newList();
+ listStorage = newStorage();
currentMusic = NULL;
isMusicInit = TRUE;
@@ -71,42 +72,35 @@ static Mix_Music* loadMixMusic(char *file)
return new;
}
-static music_t *newMusic(char *file, char *name, int group)
-{
- music_t *new;
-
- new = malloc( sizeof(music_t) );
- new->name = strdup(name);
- new->group = group;
- new->music = loadMixMusic(file);
-
- return new;
-}
-
static void playMixMusic()
{
if( currentMusic != NULL )
{
- printf("play music %s..\n", currentMusic->name);
- Mix_PlayMusic(currentMusic->music, 1000);
+ Mix_PlayMusic(currentMusic, 1000);
}
}
-static void destroyMusic(music_t *p)
+static void destroyMusic(void *p)
{
- free(p->name);
- Mix_FreeMusic(p->music);
- free(p);
+ Mix_FreeMusic((Mix_Music *)p);
}
-void addMusic(char *file, char *name, int group)
+void addMusic(char *file, char *name, char *group)
{
+ Mix_Music *new;
+
if( isMusicInit == FALSE )
{
return;
}
- addList( listMusic, newMusic(file, name, group) );
+ assert( file != NULL );
+ assert( name != NULL );
+ assert( group != NULL );
+
+ new = loadMixMusic(file);
+
+ addItemToStorage(listStorage, group, name, new );
}
void stopMusic()
@@ -119,16 +113,24 @@ void stopMusic()
if( currentMusic != NULL )
{
- printf("stop music %s..\n", currentMusic->name);
+ printf("stop music..\n");
Mix_HaltMusic();
currentMusic = NULL;
}
}
-void playMusic(char *name, int group)
+void playMusic(char *name, char *group)
{
- int i;
- music_t *this;
+ static char currentMusic_group[STR_SIZE];
+ static char currentMusic_name[STR_SIZE];
+ static int isStrInit = 0;
+
+ if( isStrInit == 0 )
+ {
+ strcpy(currentMusic_group, "none");
+ strcpy(currentMusic_name, "none");
+ isStrInit = 1;
+ }
if( isMusicInit == FALSE ||
var_isMusicActive == FALSE )
@@ -137,8 +139,8 @@ void playMusic(char *name, int group)
}
if( currentMusic != NULL &&
- currentMusic->group == group &&
- strcmp(currentMusic->name, name) == 0 )
+ strcmp(currentMusic_group, group) == 0 &&
+ strcmp(currentMusic_name, name) == 0 )
{
return;
}
@@ -148,26 +150,20 @@ void playMusic(char *name, int group)
stopMusic();
}
- for( i = 0 ; i < listMusic->count ; i++ )
- {
- this = (music_t *)listMusic->list[i];
-
- if( this->group == group && strcmp(this->name, name) == 0 )
- {
- currentMusic = this;
- break;
- }
- }
+ currentMusic = getItemFromStorage(listStorage, group, name);
+ strcpy(currentMusic_group, group);
+ strcpy(currentMusic_name, name);
if( currentMusic != NULL )
{
+ printf("play music %s\n", name);
playMixMusic();
}
}
void setMusicActive(bool_t n)
{
- static music_t *music = NULL;
+ static Mix_Music *music = NULL;
if( n == FALSE )
{
@@ -191,29 +187,12 @@ bool_t isMusicActive()
char* getCurrentMusic()
{
- return currentMusic->name;
+ return "unknown";
}
-void delAllMusicInGroup(int group)
+void delAllMusicInGroup(char *group)
{
- int i;
- music_t *this;
-
- if( isMusicInit == FALSE )
- {
- return;
- }
-
- for( i = 0 ; i < listMusic->count ; i++ )
- {
- this = (music_t *)listMusic->list[i];
-
- if( this->group == group )
- {
- delListItem(listMusic, i, destroyMusic);
- i--;
- }
- }
+ delAllItemFromStorage(listStorage, group, destroyMusic);
}
void quitMusic()
@@ -224,9 +203,8 @@ void quitMusic()
}
stopMusic();
- destroyListItem(listMusic, destroyMusic);
+ destroyStorage(listStorage, destroyMusic);
isMusicInit = FALSE;
printf("quit music..\n");
}
-
diff --git a/src/sound.c b/src/sound.c
index 3dc5552..a35d996 100644
--- a/src/sound.c
+++ b/src/sound.c
@@ -10,8 +10,9 @@
#include "string_length.h"
#include "audio.h"
#include "sound.h"
+#include "storage.h"
-static list_t *listSound;
+static list_t *listStorage;
static bool_t isSoundInit = FALSE;
static bool_t var_isSoundActive = TRUE;
@@ -29,7 +30,7 @@ void initSound()
return;
}
- listSound = newList();
+ listStorage = newStorage();
isSoundInit = TRUE;
var_isSoundActive = TRUE;
@@ -68,65 +69,48 @@ static Mix_Chunk* loadMixSound(char *file)
return new;
}
-static sound_t *newSound(char *file, char *name, int group)
-{
- sound_t *new;
-
- new = malloc( sizeof(sound_t) );
- new->name = strdup(name);
- new->group = group;
- new->sound = loadMixSound(file);
- return new;
-}
-
-static void playMixSound(sound_t *p)
+static void playMixSound(Mix_Chunk *p)
{
- if( Mix_PlayChannel(-1, p->sound, 0) == -1 )
+ if( Mix_PlayChannel(-1, p, 0) == -1 )
{
fprintf(stderr, "Nelze prehrat zvuk : %s\n", Mix_GetError());
return;
}
}
-static void destroySound(sound_t *p)
+static void destroySound(void *p)
{
- free(p->name);
- Mix_FreeChunk(p->sound);
- free(p);
+ Mix_FreeChunk((Mix_Chunk *)p);
}
-void addSound(char *file, char *name, int group)
+void addSound(char *file, char *name, char *group)
{
+ Mix_Chunk *new;
+
if( isSoundInit == FALSE )
{
return;
}
- addList( listSound, newSound(file, name, group) );
+ assert( file != NULL );
+ assert( name != NULL );
+ assert( group != NULL );
+
+ new = loadMixSound(file);
+
+ addItemToStorage(listStorage, group, name, new );
}
-void playSound(char *name, int group)
+void playSound(char *name, char *group)
{
- int i;
- sound_t *this;
-
if( isSoundInit == FALSE ||
var_isSoundActive == FALSE )
{
return;
}
- for( i = 0 ; i < listSound->count ; i++ )
- {
- this = (sound_t *)listSound->list[i];
-
- if( this->group == group && strcmp(this->name, name) == 0 )
- {
- playMixSound(this);
- return;
- }
- }
+ playMixSound( getItemFromStorage(listStorage, group, name) );
}
void setSoundActive(bool_t n)
@@ -146,7 +130,7 @@ void quitSound()
return;
}
- destroyListItem(listSound, destroySound);
+ destroyStorage(listStorage, destroySound);
isSoundInit = FALSE;
printf("quit sound..\n");
}
diff --git a/src/storage.c b/src/storage.c
new file mode 100644
index 0000000..0ae057c
--- /dev/null
+++ b/src/storage.c
@@ -0,0 +1,136 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "main.h"
+#include "list.h"
+
+typedef struct struct_storage_item
+{
+ char *name;
+ char *group;
+ void *data;
+} storage_item_t;
+
+list_t* newStorage()
+{
+ return newList();
+}
+
+static storage_item_t* newStorageItem(char *group, char *name, void *data)
+{
+ storage_item_t *new;
+
+ assert( name != NULL );
+ assert( group != NULL );
+ assert( data != NULL );
+
+ new = malloc( sizeof(storage_item_t) );
+ new->name = strdup(name);
+ new->group = strdup(group);
+ new->data = data;
+
+ return new;
+}
+
+static void destroyStorageItem(storage_item_t *p, void(*f)(void *))
+{
+ assert( p != NULL );
+ assert( f != NULL );
+
+ f(p->data);
+ free(p->name);
+ free(p->group);
+ free(p);
+}
+
+void addItemToStorage(list_t *list, char *group, char *name, void *data)
+{
+ addList(list, newStorageItem(group, name, data) );
+}
+
+void* getItemFromStorage(list_t *list, char *group, char *name)
+{
+ storage_item_t *this;
+
+ assert( group != NULL );
+ assert( name != NULL );
+
+ int i;
+
+ for(i = 0 ; i < list->count; i++)
+ {
+ this = (storage_item_t *) list->list[i];
+
+ if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 )
+ {
+ return this->data;
+ }
+ }
+
+ printf("%s %s not found in storage !!!\n", group, name);
+ return NULL;
+}
+
+void delItemFromStorage(list_t *list, char *group, char *name, void(*f)(void *))
+{
+ storage_item_t *this;
+
+ assert( group != NULL );
+ assert( name != NULL );
+
+ int i;
+
+ for(i = 0 ; i < list->count; i++)
+ {
+ this = (storage_item_t *) list->list[i];
+
+ if( strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0 )
+ {
+ destroyStorageItem(this, f);
+ delList(list, i);
+ return;
+ }
+ }
+
+ return;
+}
+
+void delAllItemFromStorage(list_t *list, char *group, void(*f)(void *))
+{
+ storage_item_t *this;
+ int i;
+
+ assert( group != NULL );
+
+ for(i = 0 ; i < list->count; i++)
+ {
+ this = (storage_item_t *) list->list[i];
+
+ if( strcmp(group, this->group) == 0 )
+ {
+ destroyStorageItem(this, f);
+ delList(list, i);
+ i--;
+ }
+ }
+}
+
+void destroyStorage(list_t *p, void(*f)(void *) )
+{
+ storage_item_t *this;
+ int i;
+
+ assert( p != NULL );
+ assert( f != NULL );
+
+ for( i = 0 ; i < p->count ; i++ )
+ {
+ this = (storage_item_t *)p->list[i];
+ destroyStorageItem(this, f);
+ }
+
+ destroyList(p);
+}