diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-11-24 21:53:26 +0000 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-11-24 21:53:26 +0000 |
commit | 122fb16bb75239bfc58805cc614701c5e248a9fa (patch) | |
tree | cd8732f391cc594af2dfe5eb107848d2b22c0e5d | |
parent | 43fceec6d8ea5f0d0ee2875341ec92403508d714 (diff) | |
download | 2010-netlemmings-122fb16bb75239bfc58805cc614701c5e248a9fa.tar.gz 2010-netlemmings-122fb16bb75239bfc58805cc614701c5e248a9fa.tar.bz2 2010-netlemmings-122fb16bb75239bfc58805cc614701c5e248a9fa.zip |
Codage de makeTerrain() dans graphic.c ; ajout test buildterrain. ça exporte tout en /bmp dans /tmp (attention, ça fait 70Mo de .bmp). Il se touve que tout est désespérément noir, comme si n'est rien blitté du tout.
git-svn-id: file:///var/svn/2010-netlemmings/trunk@172 077b3477-7977-48bd-8428-443f22f7bfda
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rwxr-xr-x | scripts/compil.sh | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/graphic.c | 64 | ||||
-rw-r--r-- | src/include/graphic.h | 10 | ||||
-rw-r--r-- | src/loader.c | 46 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/test/testfunc_003_loadress.c | 3 | ||||
-rwxr-xr-x | src/test/testfunc_003_loadress.sh | 3 | ||||
-rw-r--r-- | src/test/testfunc_004_buildterrain.c | 86 | ||||
-rwxr-xr-x | src/test/testfunc_004_buildterrain.sh | 26 |
11 files changed, 221 insertions, 25 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c596fd..7f89e94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,6 @@ add_subdirectory(src) add_subdirectory(src/test) set(ENV{LC_ALL} C) -set(ENV{CTEST_BIN} ${PROJECT_BINARY_DIR}/src/test) enable_testing() add_test(testfunc000_env ${TEST_SCRIPTS_PATH}/testfunc_000_env.sh) diff --git a/scripts/compil.sh b/scripts/compil.sh index 1df044c..c88428d 100755 --- a/scripts/compil.sh +++ b/scripts/compil.sh @@ -5,7 +5,7 @@ make if [ $# -gt 0 ] then case $1 in - test) ctest;; + test) ctest -V;; launch) gnome-terminal -t SERVEUR -e "bash -c './server_on_piou.sh ; sleep 15'"& sleep 5; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 23ea549..23b652e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ configure_file ( ) add_executable(netlem WIN32 netlem.c ${SRC_COMMON} game.c loader.c parser/lex.yy.c parser/y.tab.c ) -target_link_libraries(netlem SDL SDL_net) +target_link_libraries(netlem SDL SDL_net SDL_image) add_executable(netlem_ds WIN32 netlem_ds.c ${SRC_COMMON} ) target_link_libraries(netlem_ds SDL SDL_net) diff --git a/src/graphic.c b/src/graphic.c new file mode 100644 index 0000000..de37fc2 --- /dev/null +++ b/src/graphic.c @@ -0,0 +1,64 @@ +#include "SDL/SDL_stdinc.h" +#include "SDL/SDL_image.h" +#include "graphic.h" +#include "utils.h" + +#define SDLSURF_OPTS SDL_HWSURFACE|SDL_HWACCEL|/*SDL_ASYNCBLIT|*/SDL_RLEACCEL + +SDL_Surface * createSurface(int width, int height) { + Uint32 rmask, gmask, bmask, amask; + + /* SDL interprets each pixel as a 32-bit number, so our masks must depend + on the endianness (byte order) of the machine */ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + + return SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|/*SDL_ASYNCBLIT|*/SDL_RLEACCEL, width, height, 24, rmask, gmask, bmask, amask); + +} + +SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess) { + int res, i; + SDL_Rect dstRect; + SDL_Surface *terrain, *tile; + + terrain=createSurface(LEVEL_WIDTH, LEVEL_HEIGHT); + if (terrain==NULL) { + logs(LOG_ERROR, "makeTerrain(), SDL_CreateRGBSurface() returns NULL"); + return NULL; + } + + res=SDL_FillRect(terrain, &(terrain->clip_rect), gIni->style.bgColor); + if (res!=0) { + logs(LOG_WARN, "makeTerrain(), SDL_FillRect() failed"); + return NULL; + } + + for(i=0 ; i < gIni->level.terrainCount ; i++) { + //FIXME : check sanity for id value + tile=gRess->style.tiles[gIni->level.terrains[i].id]; + if (tile==NULL) { + logs(LOG_ERROR, "makeTerrain(), tile==NULL"); + return NULL; + } + dstRect.x=gIni->level.terrains[i].xpos; + dstRect.y=gIni->level.terrains[i].ypos; + + res=SDL_BlitSurface(tile, NULL, terrain, &dstRect); + if (res!=0) { + logs2(LOG_WARN, "makeTerrain(), SDL_BlitSurface()", SDL_GetError()); + return NULL; + } + } + + return terrain; +} diff --git a/src/include/graphic.h b/src/include/graphic.h new file mode 100644 index 0000000..697ff99 --- /dev/null +++ b/src/include/graphic.h @@ -0,0 +1,10 @@ +#ifndef GRAPHIC_H +#define GRAPHIC_H + +#include "SDL/SDL.h" +#include "data_ini.h" +#include "data_ress.h" + +SDL_Surface * makeTerrain(gameIni_t *gIni, gameRess_t *gRess); + +#endif /*GRAPHIC_H*/ diff --git a/src/loader.c b/src/loader.c index 5a34a34..bd8d8c4 100644 --- a/src/loader.c +++ b/src/loader.c @@ -1,21 +1,16 @@ #include "SDL/SDL_stdinc.h" +#include "SDL/SDL_image.h" #include "loader.h" #include "utils.h" #define PATH_STYLE "../styles" -#define ccc_keyGif 0x00000000 - -SDL_Surface *loadImage(char *filepath, Uint32 colorKey) { -//FIXME Importer le vieux load_image ici ! - return (filepath[0]+colorKey)%1; -} - int loadRessources(gameIni_t *gIni, gameRess_t *gRess) { - int i; + int i, filenamelen; char *filepath; - filepath = malloc(sizeof(char)*(strlen(PATH_STYLE)+strlen(gIni->style.name)+strlen("/om_00.gif")+1)); + filenamelen = sizeof(char)*(strlen(PATH_STYLE)+2*strlen(gIni->style.name)+strlen("//om_00.gif")+1); + filepath = malloc(filenamelen); gRess->style.tiles=malloc(sizeof(SDL_Surface *)*gIni->style.tiles); if (gRess->style.tiles==NULL) { @@ -23,10 +18,10 @@ int loadRessources(gameIni_t *gIni, gameRess_t *gRess) { return 1; } for(i=0; i < gIni->style.tiles ; ++i) { - sprintf(filepath, "%s/%s/%s_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); - gRess->style.tiles[i] = loadImage(filepath,ccc_keyGif); + snprintf(filepath, filenamelen, "%s/%s/%s_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + gRess->style.tiles[i] = IMG_Load(filepath); if(gRess->style.tiles[i]==NULL) { - logs2(LOG_WARN, "loadRessources(), loadImage() error for ", filepath); + logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath); return 2; } } @@ -45,20 +40,31 @@ int loadRessources(gameIni_t *gIni, gameRess_t *gRess) { for(i=0; i < gIni->style.objectCount ; ++i) { - sprintf(filepath, "%s/%s/%so_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); - gRess->style.objects[i] = loadImage(filepath,ccc_keyGif); + snprintf(filepath, filenamelen, "%s/%s/%so_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + gRess->style.objects[i] = IMG_Load(filepath); if (gRess->style.objects[i]==NULL) { - logs2(LOG_WARN, "loadRessources(), loadImage() error for ", filepath); + logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath); return 2; } - sprintf(filepath, "%s/%s/%som_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); - gRess->style.objectMasks[i] = loadImage(filepath,ccc_keyGif); - if (gRess->style.objectMasks[i]==NULL) { - logs2(LOG_WARN, "loadRessources(), loadImage() error for ", filepath); - return 2; + switch(gIni->style.objects[i].type) { + case 5: + case 6: + case 7: + case 8: + snprintf(filepath, filenamelen, "%s/%s/%som_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + gRess->style.objectMasks[i] = IMG_Load(filepath); + if (gRess->style.objectMasks[i]==NULL) { + logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath); + return 2; + } + break; + default: + /* No mask for other types */ + break; } } + free(filepath); return 0; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 36c21eb..0618958 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -6,4 +6,7 @@ set(PARSER_FILES ../parser/lex.yy.c ../parser/y.tab.c) add_executable(testfunc_001_lex WIN32 testfunc_001_lex.c ${PARSER_FILES} ) add_executable(testfunc_002_parse WIN32 testfunc_002_parse.c ${PARSER_FILES} ) add_executable(testfunc_003_loadress WIN32 testfunc_003_loadress.c ${PARSER_FILES} ../loader.c ../utils.c ) +target_link_libraries(testfunc_003_loadress SDL_image) +add_executable(testfunc_004_buildterrain WIN32 testfunc_004_buildterrain.c ${PARSER_FILES} ../loader.c ../graphic.c ../utils.c ) +target_link_libraries(testfunc_004_buildterrain SDL_image) diff --git a/src/test/testfunc_003_loadress.c b/src/test/testfunc_003_loadress.c index 86e85ea..7c1ed46 100644 --- a/src/test/testfunc_003_loadress.c +++ b/src/test/testfunc_003_loadress.c @@ -16,8 +16,9 @@ void load_ini(gameIni_t *gIni, char *filepath) { yyin=fopen(filepath, "r"); if (yyin == NULL ) { fprintf(stderr, "main(), Could not open '%s'\n", filepath); + exit(2); } - printf("Parsing '%s'\n", filepath); +// printf("Parsing '%s'\n", filepath); parse(gIni); fclose(yyin); } diff --git a/src/test/testfunc_003_loadress.sh b/src/test/testfunc_003_loadress.sh index a5a3bcf..b988671 100755 --- a/src/test/testfunc_003_loadress.sh +++ b/src/test/testfunc_003_loadress.sh @@ -16,10 +16,11 @@ function do_test() { find ../level -mindepth 1 -maxdepth 1 -type d | while read lp do - find $lp -type f -name *.ini | while read l + find $lp -type f -name '*.ini' -a \! -name 'levelpack.ini' | while read l do do_test src/test/testfunc_003_loadress $(basename $lp) $(basename $l) done res=$? [ $res -ne 0 ] && exit $res done +exit 0 diff --git a/src/test/testfunc_004_buildterrain.c b/src/test/testfunc_004_buildterrain.c new file mode 100644 index 0000000..9550d4a --- /dev/null +++ b/src/test/testfunc_004_buildterrain.c @@ -0,0 +1,86 @@ +#include <stdio.h> +#include <string.h> +#include "SDL/SDL.h" + +#include "parser.h" +#include "loader.h" +#include "graphic.h" +#include "utils.h" + +#define PATH_STYLE "../styles" +#define PATH_LEVEL "../level" +#define PATH_TMP "/tmp" + +#define MAX_PATH_LEN 255 +extern FILE *yyin; + +void load_ini(gameIni_t *gIni, char *filepath) { + yyin=fopen(filepath, "r"); + if (yyin == NULL ) { + fprintf(stderr, "main(), Could not open '%s'\n", filepath); + exit(2); + } +// printf("Parsing '%s'\n", filepath); + parse(gIni); + fclose(yyin); +} + +int main(int argc, char **argv) { + int res; + char filepath[MAX_PATH_LEN]; + gameIni_t gIni; + gameRess_t gRess; + SDL_Surface *terrain; + + if (argc != 3) { + fprintf(stderr, "Usage %s <levelpack_name> <ini_file>\n", argv[0]); + return 1; + } + + openLog(NULL); + + // Setting default values + memset(&gIni,0,sizeof(gameIni_t)); + + // Loading levelpack.ini + snprintf(filepath, MAX_PATH_LEN, "%s/%s/levelpack.ini", PATH_LEVEL, argv[1]); + load_ini(&gIni, filepath); + + // Loading lvl ini file + snprintf(filepath, MAX_PATH_LEN, "%s/%s/%s", PATH_LEVEL, argv[1], argv[2]); + load_ini(&gIni, filepath); + + // Check if we found a "style =" line in level ini file + if (gIni.level.style==NULL) { + fprintf(stderr, "No valid style detected\n"); + exit(1); + } + gIni.style.name=gIni.level.style; + + // Loading style ini file + snprintf(filepath, MAX_PATH_LEN, "%s/%s/%s.ini", PATH_STYLE, gIni.level.style, gIni.level.style); + load_ini(&gIni, filepath); + + + res=loadRessources(&gIni, &gRess); + + SDL_Init(SDL_INIT_VIDEO); + atexit(SDL_Quit); + + terrain=makeTerrain(&gIni, &gRess); + if (terrain==NULL) { + fprintf(stderr, "Cannot makeTerrain\n"); + exit(3); + } + + snprintf(filepath, MAX_PATH_LEN, "%s/%s_%s.bmp", PATH_TMP, argv[1], argv[2]); + res=SDL_SaveBMP(terrain, filepath); + if (res!=0) { + fprintf(stderr, "Cannot SaveBMP\n"); + exit(4); + } + + closeLog(NULL); + + return res; +} diff --git a/src/test/testfunc_004_buildterrain.sh b/src/test/testfunc_004_buildterrain.sh new file mode 100755 index 0000000..d6268d6 --- /dev/null +++ b/src/test/testfunc_004_buildterrain.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +function do_test() { + text=$($*) + res=$? + + if [ $res -ne 0 ] + then echo "Erreur : code retour $res" + exit $res + fi + if [ -n "$text" ] + then echo "Erreur : spurious text : '$text'" + exit 253 + fi +} + +find ../level -mindepth 1 -maxdepth 1 -type d | while read lp +do + find $lp -type f -name '*.ini' -a \! -name 'levelpack.ini' | while read l + do + do_test src/test/testfunc_004_buildterrain $(basename $lp) $(basename $l) + done + res=$? + [ $res -ne 0 ] && exit $res +done +exit 0 |