diff options
| author | Connor Thomson <blumatrikz@gmail.com> | 2026-09-08 18:27:39 -0700 |
|---|---|---|
| committer | Connor Thomson <blumatrikz@gmail.com> | 2026-09-08 18:27:39 -0700 |
| commit | f1ddc12b68b4e4c8087962de25260470e6df2bea (patch) | |
| tree | 7d0440a6402a9b0ef75ded23fa64b68534255bba /src | |
| parent | 6025860a61386bf767b29c3ec5fd0d31285f1056 (diff) | |
Add tuxanci2 files
Diffstat (limited to 'src')
211 files changed, 2173 insertions, 23359 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 77df5e5..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,118 +0,0 @@ -############################################################################### -# tuxanci/src/CMakeLists.txt -############################################################################### -# LINKING ( for binary ) -############################################################################### -IF ( WITH_PHYSFS ) - LINK_LIBRARIES ( ${PHYSFS_LIBRARY} ) -ELSE ( WITH_PHYSFS ) - LINK_LIBRARIES ( ${ZIP_LIBRARIES} ) -ENDIF ( WITH_PHYSFS ) -IF ( NOT BUILD_SERVER ) - IF ( WITH_OPENGL ) - LINK_LIBRARIES ( ${OPENGL_LIBRARIES} ) - ENDIF ( WITH_OPENGL ) - IF ( WITH_AUDIO ) - LINK_LIBRARIES ( - ${SDL_LIBRARIES} - ${SDLIMAGE_LIBRARIES} - ${SDLMIXER_LIBRARIES} - ${SDLTTF_LIBRARY} - ) - ELSE ( WITH_AUDIO ) - LINK_LIBRARIES ( - ${SDL_LIBRARIES} - ${SDLIMAGE_LIBRARIES} - ${SDLTTF_LIBRARY} - ) - ENDIF ( WITH_AUDIO ) - LINK_LIBRARIES ( - ${CAIRO_LIBRARIES} - ${FONTCONFIG_LIBRARIES} - ) -ENDIF ( NOT BUILD_SERVER ) -IF( WIN32 ) - LINK_LIBRARIES( -lwsock32 -lintl ) -ENDIF ( WIN32 ) -############################################################################### -# SOURCE SPECIFICATION -############################################################################### -FILE ( GLOB DIRS * ) -FOREACH ( dir ${DIRS} ) - GET_FILENAME_COMPONENT (fn ${dir} NAME) - IF ( ${fn} STREQUAL "modules" ) - ADD_SUBDIRECTORY ( ${fn} ) - ELSE ( ${fn} STREQUAL "modules" ) - INCLUDE ( ${fn}/CMakeLists.txt OPTIONAL ) - ENDIF ( ${fn} STREQUAL "modules" ) -ENDFOREACH ( dir ) -############################################################################### -# INCLUDE CONFIGURED HEADERS -############################################################################### -INCLUDE_DIRECTORIES ( ${CMAKE_BINARY_DIR}/src/base/ ) -############################################################################### -# IPv6 CHECK -############################################################################### -IF ( ENABLE_IPV6 ) - ADD_DEFINITIONS ( -DSUPPORT_IPv6 ) -ENDIF ( ENABLE_IPV6 ) -############################################################################### -# PHYSFS CHECK -############################################################################### -IF ( WITH_PHYSFS ) - ADD_DEFINITIONS ( -DSUPPORT_LIBPHYSFS ) -ELSE ( WITH_PHYSFS ) - ADD_DEFINITIONS ( -DSUPPORT_LIBZIP ) -ENDIF ( WITH_PHYSFS ) -############################################################################### -# AUDIO CHECK -############################################################################### -IF ( NOT BUILD_SERVER ) - SET ( SRC_compile ${SRC_net} - ${SRC_widget} - ${SRC_screen} - ${SRC_base} - ${SRC_client} - ) - IF ( WITH_AUDIO ) - MESSAGE ( STATUS "<Building with audio as requested>" ) - SET ( SRC_compile ${SRC_compile} ${SRC_audio} ) - INCLUDE_DIRECTORIES ( ${CMAKE_CURRENT_SOURCE_DIR}/audio ) - ELSE ( WITH_AUDIO ) - ADD_DEFINITIONS ( -DNO_SOUND ) - ENDIF ( WITH_AUDIO ) -ENDIF ( NOT BUILD_SERVER ) -############################################################################### -# OPENGL CHECK -############################################################################### -IF ( WITH_OPENGL AND NOT BUILD_SERVER ) - ADD_DEFINITIONS ( -DSUPPORT_OPENGL ) -ENDIF ( WITH_OPENGL AND NOT BUILD_SERVER ) -############################################################################### -############################################################################### -# SERVER COMPILATION -############################################################################### -IF ( BUILD_SERVER ) - MESSAGE ( STATUS "<Building server as requested>" ) - SET ( SRC_compile ${SRC_net} ${SRC_server} ${SRC_base} ) - ADD_EXECUTABLE ( ${APPNAME} ${SRC_compile} ) -ELSE ( BUILD_SERVER ) -############################################################################### -# CLIENT COMPILATION -############################################################################### - MESSAGE ( STATUS "<Building client as requested>" ) - ADD_EXECUTABLE ( ${APPNAME} ${SRC_compile} ) -ENDIF ( BUILD_SERVER ) -################################################################################ -# INSTALLATION OF BINARY -################################################################################ -FILE ( GLOB _lib_files modules/mod*.c ) -FOREACH( _lib_file ${_lib_files} ) - GET_FILENAME_COMPONENT ( _lib ${_lib_file} NAME_WE ) - ADD_DEPENDENCIES ( ${APPNAME} ${_lib} ) - TARGET_LINK_LIBRARIES ( ${APPNAME} ${_lib} ) -ENDFOREACH ( _lib_file ${_lib_files} ) -INSTALL ( TARGETS ${APPNAME} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${APPNAME} ) -################################################################################ diff --git a/src/audio.c b/src/audio.c new file mode 100644 index 0000000..0f8a337 --- /dev/null +++ b/src/audio.c @@ -0,0 +1,125 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifdef ENABLE_AUDIO + +#include <libopenmpt/libopenmpt.h> +#include "ta_sdl.h" +#include "ta_alc.h" +#include "ta_al.h" +#include "files.h" +#include "main.h" +#include "ta_openmpt.h" + +#define TA_AUDIO_FREQUENCY_HZ 44100 +#define TA_AUDIO_BUFFER_AMOUNT 4 +#define TA_AUDIO_MIXBUFFER_SAMPLES 2048 + +ALCdevice *ta_audio_device; +ALCcontext *ta_audio_context; +ALuint ta_audio_source; +ALuint ta_audio_buffers[TA_AUDIO_BUFFER_AMOUNT]; +int16_t ta_audio_mixbuffer[TA_AUDIO_MIXBUFFER_SAMPLES]; + +static openmpt_module *ta_audio_module; + +static void ta_audio_decode(void) { + size_t frame_count = TA_AUDIO_MIXBUFFER_SAMPLES / 2; + size_t rendered = openmpt_module_read_interleaved_stereo( + ta_audio_module, + TA_AUDIO_FREQUENCY_HZ, + frame_count, + ta_audio_mixbuffer + ); + + if (rendered < frame_count) { + SDL_memset( + ta_audio_mixbuffer + rendered * 2, + 0, + (frame_count - rendered) * 2 * sizeof(*ta_audio_mixbuffer) + ); + } +} + +void ta_audio_init(void) { + ta_audio_device = ta_alc_open_device(); + + ALCint attributes[] = { + ALC_FREQUENCY, TA_AUDIO_FREQUENCY_HZ, + 0 + }; + ta_audio_context = ta_alc_create_context(ta_audio_device, attributes); + ta_alc_make_context_current(ta_audio_context); + + ta_audio_module = ta_openmpt_load_module(file_data_music_theme_it_start, file_data_music_theme_it_size); + + ta_openmpt_set_module_repeat_count(ta_audio_module, -1); + + alGenSources(1, &ta_audio_source); + + alGenBuffers(TA_AUDIO_BUFFER_AMOUNT, ta_audio_buffers); + + for (int index = 0; index < TA_AUDIO_BUFFER_AMOUNT; index++) { + ta_audio_decode(); + alBufferData( + ta_audio_buffers[index], + AL_FORMAT_STEREO16, + ta_audio_mixbuffer, + sizeof(ta_audio_mixbuffer), + TA_AUDIO_FREQUENCY_HZ + ); + } + + alSourceQueueBuffers( + ta_audio_source, + TA_AUDIO_BUFFER_AMOUNT, + ta_audio_buffers + ); + alSourcePlay(ta_audio_source); +} + +void ta_audio_update(void) { + ALint processed = 0; + alGetSourcei(ta_audio_source, AL_BUFFERS_PROCESSED, &processed); + + while (processed--) { + ALuint buffer; + alSourceUnqueueBuffers(ta_audio_source, 1, &buffer); + + ta_audio_decode(); + + alBufferData(buffer, AL_FORMAT_STEREO16, ta_audio_mixbuffer, sizeof(ta_audio_mixbuffer), TA_AUDIO_FREQUENCY_HZ); + alSourceQueueBuffers(ta_audio_source, 1, &buffer); + } +} + +void ta_audio_destroy(void) { + alSourceStop(ta_audio_source); + alDeleteSources(1, &ta_audio_source); + alDeleteBuffers(TA_AUDIO_BUFFER_AMOUNT, ta_audio_buffers); + if (!ta_audio_module) { + openmpt_module_destroy(ta_audio_module); + ta_audio_module = NULL; + } + ta_alc_make_context_current(NULL); + alcDestroyContext(ta_audio_context); + ta_alc_close_device(ta_audio_device); +} + +#endif // ENABLE_AUDIO
\ No newline at end of file diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/audio/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/audio/audio.c b/src/audio/audio.c deleted file mode 100644 index 8e224fd..0000000 --- a/src/audio/audio.c +++ /dev/null @@ -1,55 +0,0 @@ -#include <stdio.h> -#include <assert.h> -#include <SDL_mixer.h> - -#include "main.h" -#include "audio.h" -#include "interface.h" - -static bool_t isAudioInit = FALSE; - -/** - * Return status of audio - */ -bool_t audio_is_inicialized() -{ - return isAudioInit; -} - -/** - * Initialize audio - */ -void audio_init() -{ - if (isParamFlag("--no-audio")) { - return; - } - - debug("Initializing audio"); - - if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) { - error("Unable to initialize audio: %s", SDL_GetError()); - return; - } - - if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) == -1) { - error("Unable to create proper audio settings: %s", Mix_GetError()); - return; - } - - Mix_AllocateChannels(16); - Mix_Volume(-1, MIX_MAX_VOLUME); - - isAudioInit = TRUE; -} - -/** - * Shutdown audio - */ -void audio_quit() -{ - debug("Shutting down audio"); - - Mix_CloseAudio(); - isAudioInit = TRUE; -} diff --git a/src/audio/audio.h b/src/audio/audio.h deleted file mode 100644 index cc0b197..0000000 --- a/src/audio/audio.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef AUDIO_H -#define AUDIO_H - -#include "main.h" - -extern bool_t audio_is_inicialized(); -extern void audio_init(); -extern void audio_quit(); - -#endif /* AUDIO_H */ diff --git a/src/audio/music.c b/src/audio/music.c deleted file mode 100644 index 9834630..0000000 --- a/src/audio/music.c +++ /dev/null @@ -1,232 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "storage.h" -#include "interface.h" -#include "audio.h" -#include "music.h" - -static list_t *listStorage; - -static bool_t isMusicInit = FALSE; -static bool_t var_music_is_active = TRUE; -static Mix_Music *currentMusic; - -/** - * Return status of music - */ -bool_t music_is_inicialized() -{ - return isMusicInit; -} - -/** - * Initialize music - */ -void music_init() -{ - if (audio_is_inicialized() == FALSE) { - isMusicInit = FALSE; - return; - } - - debug("Initializing music"); - - listStorage = storage_new(); - currentMusic = NULL; - isMusicInit = TRUE; - var_music_is_active = TRUE; - - if (isParamFlag("--no-music")) { - music_set_active(FALSE); - } else if (isParamFlag("--music")) { - music_set_active(TRUE); - } -} - -/** - * Load a file with music and returns it - */ -static Mix_Music *loadMixMusic(char *file) -{ - Mix_Music *mixer; - char str[STR_PATH_SIZE]; - - debug("Loading music [%s]", file); - - if (isFillPath(file)) { - strcpy(str, file); - } else { - sprintf(str, PATH_MUSIC "%s", file); - } - - accessExistFile(str); - - mixer = Mix_LoadMUS(str); - - if (mixer == NULL) { - error("Unable to load music [%s]", file); - return NULL; - } - - return mixer; -} - -/** - * Prepare the music mixer - */ -static void playMixMusic() -{ - if (currentMusic != NULL) { - Mix_PlayMusic(currentMusic, 1000); - } -} - -/** - * Remove music from the memory - */ -static void destroyMusic(void *p) -{ - Mix_FreeMusic((Mix_Music *) p); -} - -/** - * Add a file to the music list - */ -void music_add(char *file, char *name, char *group) -{ - Mix_Music *new; - - if (isMusicInit == FALSE) { - return; - } - - assert(file != NULL); - assert(name != NULL); - assert(group != NULL); - - new = loadMixMusic(file); - storage_add(listStorage, group, name, new); -} - -/** - * Stop playing music - */ -void music_stop() -{ - if (isMusicInit == FALSE || var_music_is_active == FALSE) { - return; - } - - if (currentMusic != NULL) { - debug("Stopping playing music"); - - Mix_HaltMusic(); - currentMusic = NULL; - } -} - -/** - * Play music playback - */ -void music_play(char *name, char *group) -{ - 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_music_is_active == FALSE) { - return; - } - - if (currentMusic != NULL && - strcmp(currentMusic_group, group) == 0 && - strcmp(currentMusic_name, name) == 0) { - return; - } - - if (currentMusic != NULL) { - music_stop(); - } - - currentMusic = storage_get(listStorage, group, name); - strcpy(currentMusic_group, group); - strcpy(currentMusic_name, name); - - if (currentMusic != NULL) { - debug("Playing music [%s]", name); - - playMixMusic(); - } -} - -/** - * Set music status to active/inactive - */ -void music_set_active(bool_t n) -{ - static Mix_Music *music = NULL; - - if (n == FALSE) { - music = currentMusic; - music_stop(); - } else if (n == TRUE) { - currentMusic = music; - playMixMusic(); - } - - var_music_is_active = n; -} - -/** - * Return the activity status of music - */ -bool_t music_is_active() -{ - return var_music_is_active; -} - -/** - * Return the music playing right now - */ -char *music_get_current() -{ - return "unknown"; -} - -/** - * Remove all music files belonging to the certain group - */ -void music_del_all_in_group(char *group) -{ - if (isMusicInit == FALSE) { - return; - } - - storage_del_all(listStorage, group, destroyMusic); -} - -/** - * Shutdown music - */ -void music_quit() -{ - if (isMusicInit == FALSE) { - return; - } - - debug("Shutting down music"); - - music_stop(); - storage_destroy(listStorage, destroyMusic); - isMusicInit = FALSE; -} diff --git a/src/audio/music.h b/src/audio/music.h deleted file mode 100644 index 9eedac7..0000000 --- a/src/audio/music.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef MUSIC_H -#define MUSIC_H - -#ifndef NO_SOUND - -#include <SDL_mixer.h> -#include "main.h" - -#define MUSIC_GROUP_BASE "base" -#define MUSIC_GROUP_USER "user" - -/* - typedef struct music_struct - { - char *name; - int group; - Mix_Music *music; - } music_t; - */ - -extern bool_t music_is_inicialized(); -extern void music_init(); -extern void music_add(char *file, char *name, char *group); -extern void music_stop(); -extern void music_play(char *name, char *group); -extern void music_set_active(bool_t n); -extern bool_t music_is_active(); -extern char *music_get_current(); -extern void music_del_all_in_group(char *group); -extern void music_quit(); - -#endif /* NO_SOUND */ -#endif /* MUSIC_H */ diff --git a/src/audio/sound.c b/src/audio/sound.c deleted file mode 100644 index 79a152d..0000000 --- a/src/audio/sound.c +++ /dev/null @@ -1,148 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "storage.h" -#include "interface.h" -#include "audio.h" -#include "sound.h" - -static list_t *listStorage; -static bool_t isSoundInit = FALSE; -static bool_t var_sound_is_active = TRUE; - -/** - * Return status of sound - */ -bool_t sound_is_inicialized() -{ - return isSoundInit; -} - -/** - * Initialize sound - */ -void sound_init() -{ - if (audio_is_inicialized() == FALSE) { - isSoundInit = FALSE; - return; - } - - debug("Initializing sound"); - - listStorage = storage_new(); - isSoundInit = TRUE; - var_sound_is_active = TRUE; - - if (isParamFlag("--no-sound")) { - sound_set_active(FALSE); - } else if (isParamFlag("--sound")) { - sound_set_active(TRUE); - } -} - -/** - * Load sound from file (Null/mixer) - */ -static Mix_Chunk *loadMixSound(char *file) -{ - Mix_Chunk *new; - char str[STR_PATH_SIZE]; - - debug("Loading sound [%s]", file); - - sprintf(str, PATH_SOUND "%s", file); - accessExistFile(str); - new = Mix_LoadWAV(str); - - if (new == NULL) { - error("Unable to load sound [%s]: %s", str, Mix_GetError()); - return NULL; - } - - return new; -} - -/** - * Play sound with mixer - */ -static void playMixSound(Mix_Chunk *p) -{ - if (Mix_PlayChannel(-1, p, 0) == -1) { - error("Unable to play sound: %s", Mix_GetError()); - return; - } -} - -/** - * Destroy mixer chunk - */ -static void destroySound(void *p) -{ - Mix_FreeChunk((Mix_Chunk *) p); -} - -/** - * Add a sound to list - */ -void sound_add(char *file, char *name, char *group) -{ - Mix_Chunk *new; - - if (isSoundInit == FALSE) { - return; - } - - assert(file != NULL); - assert(name != NULL); - assert(group != NULL); - - new = loadMixSound(file); - storage_add(listStorage, group, name, new); -} - -/** - * Start playing a sound from a file from the list - */ -void sound_play(char *name, char *group) -{ - if (isSoundInit == FALSE || var_sound_is_active == FALSE) { - return; - } - - playMixSound(storage_get(listStorage, group, name)); -} - -/** - * Set sound status to active/inactive (true/false) - */ -void sound_set_active(bool_t n) -{ - var_sound_is_active = n; -} - -/** - * Return status of sound - */ -bool_t sound_is_active() -{ - return var_sound_is_active; -} - -/** - * Shutdown sound - */ -void sound_quit() -{ - if (isSoundInit == FALSE) { - return; - } - - debug("Shutting down sound"); - - storage_destroy(listStorage, destroySound); - isSoundInit = FALSE; -} diff --git a/src/audio/sound.h b/src/audio/sound.h deleted file mode 100644 index f8cf2d3..0000000 --- a/src/audio/sound.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef SOUND_H -#define SOUND_H - -#ifndef NO_SOUND - -#include <SDL_mixer.h> -#include "main.h" - -#define SOUND_GROUP_BASE "base" -#define SOUND_GROUP_USER "user" - -/* - typedef struct sound_struct - { - char *name; - int group; - Mix_Chunk *sound; - } sound_t; - */ - -extern bool_t sound_is_inicialized(); -extern void sound_init(); -extern void sound_add(char *file, char *name, char *group); -extern void sound_play(char *name, char *group); -extern void sound_set_active(bool_t n); -extern bool_t sound_is_active(); -extern void sound_quit(); - -#endif /* NO_SOUND */ -#endif /* SOUND_H */ diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/base/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/base/archive.c b/src/base/archive.c deleted file mode 100644 index 2c188c3..0000000 --- a/src/base/archive.c +++ /dev/null @@ -1,143 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#ifdef SUPPORT_LIBPHYSFS -#include <physfs.h> -#endif /* SUPPORT_LIBPHYSFS */ - -#ifdef SUPPORT_LIBZIP -#include <zip.h> -#endif /* SUPPORT_LIBZIP */ - -#include "main.h" -#include "archive.h" - -#define ARCHIVE_BUFFER_SIZE 4096 -#define STR_TMP_FILE_NAME 4096 - -#ifdef SUPPORT_LIBZIP -char *archive_extract_file(char *archive, char *filename) -{ - char name[STR_TMP_FILE_NAME]; - char buf[ARCHIVE_BUFFER_SIZE]; - - struct zip *zipArchive; - struct zip_file *zipFile; - - FILE *file; - int len; - int err; - char *tmp; - -#ifndef __WIN32__ - tmp = getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"); -#else /* __WIN32__ */ - tmp = getenv("TEMP"); -#endif /* __WIN32__ */ - sprintf(name, "%s%stuxanci.%d-%s", tmp, PATH_SEPARATOR, getpid(), filename); - - /*printf("ARCHIVE %s %s %s\n", archive, filename, name);*/ - - zipArchive = zip_open(archive, 0, &err); - - if (zipArchive == NULL) { - error("Unable to open archive [%s]", archive); - return NULL; - } - - zipFile = zip_fopen(zipArchive, filename, ZIP_FL_UNCHANGED); - - if (zipFile == NULL) { - error("Unable to extract the file [%s] from archive [%s]", filename, archive); - zip_close(zipArchive); - return NULL; - } - - if ((file = fopen(name, "wb")) == NULL) { - error("Unable to create temporary file [%s]", name); - zip_fclose(zipFile); - zip_close(zipArchive); - return NULL; - } - - do { - len = zip_fread(zipFile, buf, ARCHIVE_BUFFER_SIZE); - - if (len > 0) { - fwrite(buf, len, 1, file); - } - } while (len > 0); - - fclose(file); - zip_fclose(zipFile); - zip_close(zipArchive); - - return strdup(name); -} -#endif /* SUPPORT_LIBZIP */ - -#ifdef SUPPORT_LIBPHYSFS -char *archive_extract_file(char *archive, char *filename) -{ - char name[STR_TMP_FILE_NAME]; - char buffer[ARCHIVE_BUFFER_SIZE]; - - PHYSFS_file *file_archive; - FILE *file; - int count; - - char *tmp; - -#ifndef __WIN32__ - tmp = getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"); -#else /* __WIN32__ */ - tmp = getenv("TEMP"); -#endif /* __WIN32__ */ - sprintf(name, "%s%stuxanci.%d-%s", tmp, PATH_SEPARATOR, getpid(), filename); - - if (!(PHYSFS_init(get_program_name()))) { - error("Unable to init physfs"); - return NULL; - } - - if (!(PHYSFS_addToSearchPath(archive, 1))) { - error("Unable to open archive [%s]", archive); - PHYSFS_deinit(); - return NULL; - } - - file_archive = PHYSFS_openRead(filename); - - if (file_archive == NULL) { - error("Unable to extract the file [%s] from archive [%s]", filename, archive); - PHYSFS_deinit(); - return NULL; - } - - if ((file = fopen(name, "wb")) == NULL) { - error("Unable to create temporary file [%s]", name); - PHYSFS_close(file_archive); - PHYSFS_deinit(); - return NULL; - } - - while ((count = PHYSFS_read(file_archive, buffer, 1, ARCHIVE_BUFFER_SIZE)) > 0) { - fwrite(buffer, count, 1, file); - } - - PHYSFS_close(file_archive); - fclose(file); - - PHYSFS_deinit(); - - return strdup(name); -} -#endif /* SUPPORT_LIBPHYSFS */ - -void archive_delete_file(char *s) -{ - unlink(s); - free(s); -} diff --git a/src/base/archive.h b/src/base/archive.h deleted file mode 100644 index e2ad361..0000000 --- a/src/base/archive.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef ARCHIVE_H -#define ARCHIVE_H - -extern char *archive_extract_file(char *archive, char *file); -extern void archive_delete_file(char *s); - -#endif /* ARCHIVE_H */ diff --git a/src/base/arena.c b/src/base/arena.c deleted file mode 100644 index 6d883b3..0000000 --- a/src/base/arena.c +++ /dev/null @@ -1,392 +0,0 @@ -#include <stdlib.h> -#include <assert.h> -#include <string.h> - -#include "main.h" -#include "arena.h" -#include "list.h" -#include "tux.h" -#include "shot.h" -#include "item.h" -#include "myTimer.h" -#include "modules.h" - -#ifndef PUBLIC_SERVER -#include "hotKey.h" -#include "world.h" -#include "setting.h" -#include "layer.h" -#endif /* PUBLIC_SERVER */ - -static arena_t *currentArena; -static int splitType; - -#ifndef PUBLIC_SERVER -static int isBigArena(arena_t *arena) -{ - if (arena->w > WINDOW_SIZE_X || arena->h > WINDOW_SIZE_Y) { - return 1; - } else { - return 0; - } -} - -static int isTuxNear(tux_t *tux1, tux_t *tux2) -{ - if (abs(tux1->x - tux2->x) < WINDOW_SIZE_X && - abs(tux1->y - tux2->y) < WINDOW_SIZE_Y) { - return 1; - } - - return 0; -} -#endif /* PUBLIC_SERVER */ - -void arena_set_current(arena_t *p) -{ - currentArena = p; -} - -arena_t *arena_get_current() -{ - return currentArena; -} - -void hotkey_splitArena() -{ - if (splitType == SCREEN_SPLIT_VERTICAL) { - splitType = SCREEN_SPLIT_HORIZONTAL; - } else { - splitType = SCREEN_SPLIT_VERTICAL; - } -} - -#ifndef PUBLIC_SERVER -void arena_init() -{ - splitType = SCREEN_SPLIT_VERTICAL; - - if (getParam("--split-vertical")) { - splitType = SCREEN_SPLIT_VERTICAL; - debug("Set vertical screen splitting"); - } - - if (getParam("--split-horizontal")) { - splitType = SCREEN_SPLIT_HORIZONTAL; - debug("Set horizontal screen splitting"); - } - - hot_key_register(SDLK_F3, hotkey_splitArena); -} - -void arena_quit() -{ - hot_key_unregister(SDLK_F3); -} -#endif /* PUBLIC_SERVER */ - -arena_t *arena_new(int w, int h) -{ - arena_t *new; - int zone_w, zone_h; - - new = malloc(sizeof(arena_t)); - -#ifndef PUBLIC_SERVER - new->background = NULL; - strcpy(new->music, ""); -#endif /* PUBLIC_SERVER */ - - new->w = w; - new->h = h; - - new->listTimer = list_new(); - - if (w > 800 || h > 600) { - zone_w = 320; - zone_h = 240; - } else { - zone_w = 80; - zone_h = 60; - } - - new->spaceTux = space_new(w, h, zone_w, zone_h, tux_get_status, tux_set_status); - new->spaceItem = space_new(w, h, zone_w, zone_h, item_get_status, item_set_status); - new->spaceShot = space_new(w, h, zone_w, zone_h, shot_get_status, shot_set_status); - - new->countRound = 0; - new->max_countRound = 0; - - return new; -} - -int arena_conflict_space(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) -{ - return (x1 < x2 + w2 && x2 < x1 + w1 && y1 < y2 + h2 && y2 < y1 + h1); -} - -int arena_is_free_space(arena_t *arena, int x, int y, int w, int h) -{ - if ((x < 0 || y < 0 || x + w > arena->w || y + h > arena->h) || - space_is_conflict_with_object(arena->spaceTux, x, y, w, h) || - space_is_conflict_with_object(arena->spaceShot, x, y, w, h) || - space_is_conflict_with_object(arena->spaceItem, x, y, w, h) || - space_is_conflict_with_object(arena->spaceShot, x, y, w, h) || - module_is_conflict(x, y, w, h)) { - return 0; - } - - return 1; -} - -void arena_find_free_space(arena_t *arena, int *x, int *y, int w, int h) -{ - int z_x; - int z_y; - - assert(x != NULL); - assert(y != NULL); - - do { - z_x = random() % (arena->w - w); - z_y = random() % (arena->h - h); - } while (arena_is_free_space(arena, z_x, z_y, w, h) == 0); - - *x = z_x; - *y = z_y; -} - -void arena_get_center_screen(int *screen_x, int *screen_y, int x, int y, int screen_size_x, int screen_size_y) -{ - arena_t *arena; - - arena = arena_get_current(); - - *screen_x = x - screen_size_x / 2; - *screen_y = y - screen_size_y / 2; - - if (*screen_x < 0) { - *screen_x = 0; - } - - if (*screen_y < 0) { - *screen_y = 0; - } - - if (*screen_x + screen_size_x >= arena->w) { - *screen_x = arena->w - screen_size_x; - } - - if (*screen_y + screen_size_y >= arena->h) { - *screen_y = arena->h - screen_size_y; - } -} - -#ifndef PUBLIC_SERVER -static void drawBackground(arena_t *arena, int screen_x, int screen_y) -{ - if (isBigArena(arena)) { - int i, j; - - for (i = screen_y / arena->background->h; - i <= screen_y / arena->background->h + WINDOW_SIZE_Y / arena->background->h + 1; - i++) { - for (j = screen_x / arena->background->w; - j <= screen_x / arena->background->w + WINDOW_SIZE_X / arena->background->w + 1; - j++) { - addLayer(arena->background, j * arena->background->w, - i * arena->background->h, 0, 0, arena->background->w, - arena->background->h, -100); - - /*count++;*/ - } - } - } else { - addLayer(arena->background, 0, 0, 0, 0, arena->background->w, arena->background->h, -100); - } -} - -static void action_tux_draw(space_t *space, tux_t *tux, void *p) -{ - tux_draw(tux); -} - -static void action_item_draw(space_t *space, item_t *item, void *p) -{ - item_draw(item); -} - -static void action_shot_draw(space_t *space, shot_t *shot, void *p) -{ - shot_draw(shot); -} - -static void drawObjects(arena_t *arena, int screen_x, int screen_y) -{ - space_action_from_location(arena->spaceTux, action_tux_draw, NULL, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); - space_action_from_location(arena->spaceItem, action_item_draw, NULL, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); - space_action_from_location(arena->spaceShot, action_shot_draw, NULL, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); - - module_draw(screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); -} - -static void drawSplitArenaForTux(arena_t *arena, tux_t *tux, int location_x, int location_y) -{ - int screen_x, screen_y; - - switch (splitType) { - case SCREEN_SPLIT_HORIZONTAL: - arena_get_center_screen(&screen_x, &screen_y, tux->x, tux->y, WINDOW_SIZE_X, WINDOW_SIZE_Y / 2); - break; - case SCREEN_SPLIT_VERTICAL: - arena_get_center_screen(&screen_x, &screen_y, tux->x, tux->y, WINDOW_SIZE_X / 2, WINDOW_SIZE_Y); - break; - default: - screen_x = -1; - screen_y = -1; - fatal("Unknown method of splitting the screen"); - break; - } - - drawBackground(arena, screen_x, screen_y); - drawObjects(arena, screen_x, screen_y); - - switch (splitType) { - case SCREEN_SPLIT_HORIZONTAL: - layer_draw_slpit(location_x, location_y, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y / 2); - break; - case SCREEN_SPLIT_VERTICAL: - layer_draw_slpit(location_x, location_y, screen_x, screen_y, WINDOW_SIZE_X / 2, WINDOW_SIZE_Y); - break; - default: - screen_x = -1; - screen_y = -1; - fatal("Unknown method of splitting the screen"); - break; - } -} - -void drawSplitArena(arena_t *arena) -{ - tux_t *tux = NULL; - - tux = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - - if (tux != NULL) { - switch (splitType) { - case SCREEN_SPLIT_HORIZONTAL: - drawSplitArenaForTux(arena, tux, 0, WINDOW_SIZE_Y / 2); - break; - case SCREEN_SPLIT_VERTICAL: - drawSplitArenaForTux(arena, tux, WINDOW_SIZE_X / 2, 0); - break; - } - } - - tux = world_get_control_tux(TUX_CONTROL_KEYBOARD_LEFT); - - if (tux != NULL) { - drawSplitArenaForTux(arena, tux, 0, 0); - } -} - -void drawCenterArena(arena_t *arena, int x, int y) -{ - int screen_x, screen_y; - - arena_get_center_screen(&screen_x, &screen_y, x, y, WINDOW_SIZE_X, WINDOW_SIZE_Y); - drawBackground(arena, screen_x, screen_y); - drawObjects(arena, screen_x, screen_y); - layer_draw_center(x, y); -} - -void drawSimpleArena(arena_t *arena) -{ - int screen_x, screen_y; - tux_t *tux = NULL; - - tux = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - - if (tux == NULL) { - return; - } - - screen_x = 0; - screen_y = 0; - - drawBackground(arena, screen_x, screen_y); - drawObjects(arena, screen_x, screen_y); - layer_draw_all(tux->x, tux->y); -} - -void arena_draw(arena_t *arena) -{ - if (isBigArena(arena) && - net_multiplayer_get_game_type() == NET_GAME_TYPE_NONE && !setting_is_ai()) { - tux_t *tux1; - tux_t *tux2; - - tux1 = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - tux2 = world_get_control_tux(TUX_CONTROL_KEYBOARD_LEFT); - - if (isTuxNear(tux1, tux2)) { - drawCenterArena(arena, (tux1->x + tux2->x) / 2, (tux1->y + tux2->y) / 2); - } else { - drawSplitArena(arena); - } - - return; - } - - if (isBigArena(arena)) { - tux_t *tux = NULL; - - tux = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - - if (tux == NULL) { - return; - } - - drawCenterArena(arena, tux->x, tux->y); - return; - } - - drawSimpleArena(arena); -} - -#endif /* PUBLIC_SERVER */ - -static void action_tux(space_t *space, tux_t *tux, void *p) -{ - tux_event(tux); -} - -void arena_event(arena_t *arena) -{ - int i; - - for (i = 0; i < 8; i++) { - shot_check_is_tux_screen(arena); - - item_event_conglicts_shot_with_item(arena); - tux_conflict_woth_shot(arena); - module_event(); - - shot_event_move_list(arena); - } - - item_event(arena->spaceItem); - - space_action(arena->spaceTux, action_tux, NULL); - - timer_event(arena->listTimer); -} - -void arena_destroy(arena_t *p) -{ - space_destroy_with_item(p->spaceTux, tux_destroy); - space_destroy_with_item(p->spaceItem, item_destroy); - space_destroy_with_item(p->spaceShot, shot_destroy); - timer_destroy(p->listTimer); - free(p); -} diff --git a/src/base/arena.h b/src/base/arena.h deleted file mode 100644 index a01484a..0000000 --- a/src/base/arena.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef ARENA_H -#define ARENA_H - -#include "main.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#endif /* PUBLIC_SERVER */ - -#include "list.h" -#include "space.h" - -#ifdef __WIN32__ -#define random rand -#endif /* __WIN32__ */ - -#define SCREEN_SPLIT_HORIZONTAL 1 -#define SCREEN_SPLIT_VERTICAL 2 - -typedef struct arena_struct { -#ifndef PUBLIC_SERVER - char music[STR_SIZE]; - - image_t *background; -#endif /* PUBLIC_SERVER */ - int w, h; - - list_t *listTimer; - - space_t *spaceShot; - space_t *spaceTux; - space_t *spaceItem; - - int countRound; - int max_countRound; -} arena_t; - -extern void arena_set_current(arena_t *p); -extern arena_t *arena_get_current(); -extern void arena_init(); -extern void arena_quit(); -extern arena_t *arena_new(int w, int h); -extern int arena_conflict_space(int x1, int y1, int w1, int h1, - int x2, int y2, int w2, int h2); -extern int arena_is_free_space(arena_t *arena, int x, int y, int w, int h); -extern void arena_find_free_space(arena_t *arena, int *x, int *y, int w, int h); -extern void arena_get_center_screen(int *screen_x, int *screen_y, int x, int y, - int screen_size_x, int screen_size_y); -#ifndef PUBLIC_SERVER -extern void arena_draw(arena_t *arena); -#endif /* PUBLIC_SERVER */ -extern void arena_event(arena_t *arena); -extern void arena_destroy(arena_t *p); - -#endif /* ARENA_H */ diff --git a/src/base/arenaFile.c b/src/base/arenaFile.c deleted file mode 100644 index b5ef6a2..0000000 --- a/src/base/arenaFile.c +++ /dev/null @@ -1,413 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "arenaFile.h" -#include "arena.h" -#include "tux.h" -#include "shot.h" -#include "item.h" -#include "director.h" -#include "modules.h" -#include "archive.h" -#include "homeDirector.h" - -#ifndef PUBLIC_SERVER -#include "configFile.h" -#include "image.h" -#endif /* PUBLIC_SERVER */ - -#ifndef NO_SOUND -#include "music.h" -#endif /* NO_SOUND */ - -static list_t *listArenaFile; -static bool_t isArenaFileInit = FALSE; - -bool_t arena_file_is_inicialized() -{ - return isArenaFileInit; -} - -int arena_file_get_value(char *line, char *env, char *val, int len) -{ - char *offset_env; - char *offset_val_begin; - char *offset_val_end; - char clone_env[STR_SIZE]; - int val_len; - - strcpy(clone_env, " "); - strcat(clone_env, env); - - offset_env = strstr(line, clone_env); - - if (offset_env == NULL) { - return -1; - } - - offset_val_begin = strchr(offset_env, '"'); - - if (offset_val_begin == NULL) { - return -1; - } - - offset_val_end = strchr(offset_val_begin + 1, '"'); - - if (offset_val_end == NULL) { - return -1; - } - - val_len = (int) (offset_val_end - (offset_val_begin + 1)); - - if (val_len > len - 1) { - val_len = len - 1; - } - - memset(val, 0, len); - memcpy(val, offset_val_begin + 1, val_len); - - return 0; -} - - -#ifndef PUBLIC_SERVER -image_t *arena_file_load_image(arenaFile_t *arenaFile, char *filename, char *group, char *name, int alpha) -{ - char *extractFile; - image_t *image; - - extractFile = archive_extract_file(arenaFile->path, filename); - image = image_add(extractFile, alpha, name, group); - archive_delete_file(extractFile); - - return image; -} - -#ifndef NO_SOUND -void loadMusicFromArena(arenaFile_t *arenaFile, char *filename, char *group, char *name) -{ - char *extractFile; - - extractFile = archive_extract_file(arenaFile->path, filename); - music_add(extractFile, name, group); - archive_delete_file(extractFile); -} -#endif /* NO_SOUND */ -#endif /* PUBLIC_SERVER */ - -static void cmd_arena(arena_t **arena, char *line) -{ - char str_image[STR_SIZE]; - char str_w[STR_SIZE]; - char str_h[STR_SIZE]; - - if (arena_file_get_value(line, "background", str_image, STR_SIZE) != 0) { - return; - } - - if (arena_file_get_value(line, "w", str_w, STR_SIZE) != 0) { - return; - } - - if (arena_file_get_value(line, "h", str_h, STR_SIZE) != 0) { - return; - } - - (*arena) = arena_new(atoi(str_w), atoi(str_h)); -#ifndef PUBLIC_SERVER - (*arena)->background = image_get(IMAGE_GROUP_USER, str_image); -#endif /* PUBLIC_SERVER */ - /* - (*arena)->w = atoi(str_w); - (*arena)->h = atoi(str_h); - */ - arena_set_current(*arena); - - debug("Arena loaded"); -} - -static void cmd_module_load(char *line) -{ - char str_file[STR_SIZE]; - - if (arena_file_get_value(line, "file", str_file, STR_SIZE) != 0) { - return; - } - - module_load(str_file); -} - -#ifndef PUBLIC_SERVER -static void cmd_loadImage(arenaFile_t *arenaFile, char *line) -{ - char str_file[STR_SIZE]; - char str_name[STR_SIZE]; - char str_alpha[STR_SIZE]; - - if (arena_file_get_value(line, "file", str_file, STR_SIZE) != 0) { - return; - } - - if (arena_file_get_value(line, "name", str_name, STR_SIZE) != 0) { - return; - } - - if (arena_file_get_value(line, "alpha", str_alpha, STR_SIZE) != 0) { - return; - } - - /*printf("str_file=%s str_name=%s str_alpha=%s\n", str_file, str_name, str_alpha);*/ - arena_file_load_image(arenaFile, str_file, IMAGE_GROUP_USER, str_name, isYesOrNO(str_alpha)); - - /*image_add(str_file, isYesOrNO(str_alpha), str_name, IMAGE_GROUP_USER);*/ -} - -static void cmd_loadMusic(arenaFile_t *arenaFile, char *line) -{ - char str_file[STR_SIZE]; - char str_name[STR_SIZE]; - - if (arena_file_get_value(line, "file", str_file, STR_SIZE) != 0) { - return; - } - - if (arena_file_get_value(line, "name", str_name, STR_SIZE) != 0) { - return; - } - -#ifndef NO_SOUND - /*music_add(str_file, str_name, MUSIC_GROUP_USER);*/ - loadMusicFromArena(arenaFile, str_file, MUSIC_GROUP_USER, str_name); -#endif /* NO_SOUND */ -} - -static void cmd_music_play(arena_t *arena, char *line) -{ - char str_music[STR_SIZE]; - - if (arena_file_get_value(line, "music", str_music, STR_SIZE) != 0) { - return; - } - - strcpy(arena->music, str_music); -} -#endif /* PUBLIC_SERVER */ - -int arena_file_get_count() -{ - return listArenaFile->count; -} - -static char *arena_file_get_status(textFile_t *ts, char *s) -{ - int len; - int i; - - len = strlen(s); - - for (i = 0; i < ts->text->count; i++) { - char *line; - - line = (char *) (ts->text->list[i]); - - if (strncmp(line, s, len) == 0) { - return line + len + 1; - } - } - - return NULL; -} - -char *arena_file_get_name(arenaFile_t *arenaFile) -{ - char *ret; - - ret = arena_file_get_status(arenaFile->map, "name"); - return (ret != NULL ? ret : "arena_no_name"); -} - -char *arena_file_get_net_name(arenaFile_t *arenaFile) -{ - char *ret; - - ret = arena_file_get_status(arenaFile->map, "netName"); - return (ret != NULL ? ret : "arena_no_net_name"); -} - -arenaFile_t *arena_file_get_file_format_net_name(char *s) -{ - int i; - - for (i = 0; i < listArenaFile->count; i++) { - arenaFile_t *arenaFile; - - arenaFile = (arenaFile_t *) listArenaFile->list[i]; - - if (strcmp(arena_file_get_net_name(arenaFile), s) == 0) { - return arenaFile; - } - } - - return NULL; -} - -char *arena_file_get_image(arenaFile_t *arenaFile) -{ - return arena_file_get_status(arenaFile->map, "screen"); -} - -arenaFile_t *arena_file_get(int n) -{ - return (arenaFile_t *) listArenaFile->list[n]; -} - -int arena_file_get_id(arenaFile_t *arenaFile) -{ - int i; - - for (i = 0; i < listArenaFile->count; i++) { - arenaFile_t *this; - - this = (arenaFile_t *) listArenaFile->list[i]; - - if (this == arenaFile) { - return i; - } - } - - return -1; -} - -arena_t *arena_file_get_arena(arenaFile_t *arenaFile) -{ - textFile_t *ts; - arena_t *arena = NULL; /* no warnings */ - int i; - - ts = (textFile_t *) arenaFile->map; - - for (i = 0; i < ts->text->count; i++) { - char *line; - - line = (char *) ts->text->list[i]; - module_cmd(line); -#ifndef PUBLIC_SERVER - if (strncmp(line, "loadImage", 9) == 0) { - cmd_loadImage(arenaFile, line); - } - - if (strncmp(line, "loadMusic", 9) == 0) { - cmd_loadMusic(arenaFile, line); - } - - if (strncmp(line, "playMusic", 9) == 0) { - cmd_music_play(arena, line); - } -#endif /* PUBLIC_SERVER */ - - if (strncmp(line, "arena", 5) == 0) { - cmd_arena(&arena, line); - } - - if (strncmp(line, "loadModule", 10) == 0) { - cmd_module_load(line); - } - } - - return arena; -} - -arenaFile_t *arena_file_new(char *path) -{ - arenaFile_t *new; - char *extractFile; - - new = malloc(sizeof(arenaFile_t)); - new->path = strdup(path); - - extractFile = archive_extract_file(path, "arena.map"); - new->map = text_file_load(extractFile); - archive_delete_file(extractFile); - - return new; -} - -void arena_destroyFile(arenaFile_t *p) -{ - text_file_destroy(p->map); - free(p->path); - free(p); -} - -void arena_file_load(char *path) -{ - list_add(listArenaFile, arena_file_new(path)); -} - -static void load_arenaFromDirector(char *director) -{ - director_t *p; - int i; - - debug("Loading arena files [%s]", director); - p = director_load(director); - - if (p == NULL) { - error("Directory not found [%s]", director); - exit(-1); - } - - unsigned l = strlen(director) - 1; - - for (i = 0; i < p->list->count; i++) { - char *line; - - line = (char *) p->list->list[i]; - - if (strstr(line, ".zip") != NULL && strstr(line, "~") == NULL) { - char path[STR_PATH_SIZE]; - - - if (director[l] == (char) PATH_SEPARATOR[0]) { - sprintf(path, "%s%s", director, line); - } else { - sprintf(path, "%s%s%s", director, PATH_SEPARATOR, line); - } - - debug("Loading arena [%s]", line); - - accessExistFile(path); - arena_file_load(path); - } - } - - /*printf("No. of Arens: %d\n", listArenaFile->count);*/ - director_destroy(p); - -} - -void arena_file_init() -{ -#ifndef PUBLIC_SERVER - assert(image_is_inicialized() == TRUE); -#endif /* PUBLIC_SERVER */ - isArenaFileInit = TRUE; - listArenaFile = list_new(); - - load_arenaFromDirector(PATH_ARENA); - -#ifndef PUBLIC_SERVER - load_arenaFromDirector(home_director_get()); -#endif /* PUBLIC_SERVER */ -} - -void arena_file_quit() -{ - isArenaFileInit = FALSE; - list_destroy_item(listArenaFile, arena_destroyFile); -} diff --git a/src/base/arenaFile.h b/src/base/arenaFile.h deleted file mode 100644 index 40a9f5f..0000000 --- a/src/base/arenaFile.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef ARENA_FILE_H -#define ARENA_FILE_H - -#define PUBLIC_SERVER_PATH_ARENA "./arena/" - -#include "main.h" -#include "arena.h" -#include "textFile.h" - -typedef struct { - char *path; - textFile_t *map; -} arenaFile_t; - -extern bool_t arena_file_is_inicialized(); -extern int arena_file_get_value(char *line, char *env, char *val, int len); -extern int arena_file_get_count(); -extern char *arena_file_get_name(arenaFile_t *arenaFile); -extern char *arena_file_get_net_name(arenaFile_t *arenaFile); -extern arenaFile_t *arena_file_get_file_format_net_name(char *s); -extern char *arena_file_get_image(arenaFile_t *arenaFile); -#ifndef PUBLIC_SERVER -extern image_t *arena_file_load_image(arenaFile_t *arenaFile, char *filename, - char *group, char *name, int alpha); -#endif /* PUBLIC_SERVER */ -extern arenaFile_t *arena_file_get(int n); -extern int arena_file_get_id(arenaFile_t *arenaFile); -extern arena_t *arena_file_get_arena(arenaFile_t *arenaFile); -extern arenaFile_t *arena_file_new(char *path); -extern void arena_file_load(char *path); -extern void arena_file_init(); -extern void arena_file_quit(); - -#endif /* ARENA_FILE_H */ diff --git a/src/base/bool.h b/src/base/bool.h deleted file mode 100644 index 76a3756..0000000 --- a/src/base/bool.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef BOOL_H -#define BOOL_H - -#include "main.h" - -#define bool_t int -#define TRUE 1 -#define FALSE 0 - -#endif /* BOOL_H */ diff --git a/src/base/buffer.c b/src/base/buffer.c deleted file mode 100644 index 2ecc148..0000000 --- a/src/base/buffer.c +++ /dev/null @@ -1,142 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "buffer.h" - -buffer_t *buffer_new(int n) -{ - buffer_t *new; - - new = malloc(sizeof(buffer_t)); - memset(new, 0, sizeof(buffer_t)); - new->alloc = n; - new->data = malloc(n); - memset(new->data, 0, n); - - return new; -} - -void *buffer_get_data(buffer_t *p) -{ - return p->data; -} - -int buffer_get_size(buffer_t *p) -{ - return p->size; -} - -int buffer_append(buffer_t *p, char *data, int len) -{ - assert(p != NULL); - assert(data != NULL); - assert(len >= 0); - - if (p->size + len <= p->alloc) { - memcpy(p->data + p->size, data, len); - p->size += len; - return len; - } - - return -1; -} - -int buffer_cut(buffer_t *p, int len) -{ - assert(p != NULL); - assert(len >= 0); - - if (p->size - len >= 0) { - memmove(p->data, p->data + len, p->size - len); - p->size -= len; - return 0; - } - - return -1; -} - -int buffer_count_line(buffer_t *p) -{ - char *begin; - char *end; - int count; - int size; - - assert(p != NULL); - - count = 0; - begin = p->data; - size = p->size; - - while ((end = memchr(begin, '\n', size)) != NULL) { - count++; - size -= ((end + 1) - begin); - begin = end + 1; - } - - /* - printf("count = %d\n", count); - printf("data = %s\n", p->data); - */ - - return count; -} - -int buffer_get_line(buffer_t *p, char *line, int len) -{ - char *end; - int ret_len; - - assert(p != NULL); - assert(line != NULL); - assert(len >= 0); - - end = NULL; - - end = memchr(p->data, '\n', p->size); - - if (end == NULL) { - return -1; - } - - ret_len = (int) (end - p->data) + 1; - if (ret_len > len - 1) { - ret_len = len - 1; - } - - memset(line, 0, len); - memcpy(line, p->data, ret_len); - - buffer_cut(p, ret_len); - - return ret_len; -} - -int buffer_get_data_len(buffer_t *p, char *line, int len) -{ - assert(p != NULL); - assert(line != NULL); - assert(len >= 0); - - if ((unsigned)p->size < (unsigned)len) { - len = p->size; - } - - memcpy(line, p->data, len); - buffer_cut(p, len); - - return len; -} - -void buffer_destroy(buffer_t *p) -{ - assert(p != NULL); - - if (p->data != NULL) { - free(p->data); - } - - free(p); -} diff --git a/src/base/buffer.h b/src/base/buffer.h deleted file mode 100644 index 9ddc5e6..0000000 --- a/src/base/buffer.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef BUFFER_H -#define BUFFER_H - -#define BUFFER_LIMIT_ALLOC 1024 - -typedef struct str_buffer { - char *data; - unsigned int size; - unsigned int alloc; -} buffer_t; - -extern buffer_t *buffer_new(int n); -extern void *buffer_get_data(buffer_t *p); -extern int buffer_get_size(buffer_t *p); -extern int buffer_append(buffer_t *p, char *data, int len); -extern int buffer_cut(buffer_t *p, int len); -extern int buffer_count_line(buffer_t *p); -extern int buffer_get_line(buffer_t *p, char *line, int len); -extern int buffer_get_data_len(buffer_t *p, char *line, int len); -extern void buffer_destroy(buffer_t *p); - -#endif /* BUFFER_H */ diff --git a/src/base/checkFront.c b/src/base/checkFront.c deleted file mode 100644 index 999eb58..0000000 --- a/src/base/checkFront.c +++ /dev/null @@ -1,117 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "myTimer.h" -#include "checkFront.h" -#include "idManager.h" -#include "server.h" - -typedef struct struct_checkfront_t { - char *msg; - my_time_t time; - int id; - int count; - int type; -} checkfront_t; - -static checkfront_t *newCheck(char *s, int type, int id) -{ - checkfront_t *new; - - assert(s != NULL); - - new = malloc(sizeof(checkfront_t)); - memset(new, 0, sizeof(checkfront_t)); - new->msg = strdup(s); - new->time = timer_get_current_time(); - new->id = id; - new->type = type; - new->count = 0; - - return new; -} - -static void destroyCheck(checkfront_t *p) -{ - assert(p != NULL); - free(p->msg); - free(p); -} - -list_t *check_front_new() -{ - return list_new(); -} - -void check_front_msg_add(list_t *list, char *msg, int type, int id) -{ - if (type == CHECK_FRONT_TYPE_CHECK) { - id_inc(id); - } - - list_add(list, newCheck(msg, type, id)); -} - -void check_front_event(client_t *client) -{ - my_time_t currentTime; - int i; - - currentTime = timer_get_current_time(); - - for (i = 0; i < client->listSendMsg->count; i++) { - checkfront_t *this; - - this = (checkfront_t *) client->listSendMsg->list[i]; - - switch (this->type) { - case CHECK_FRONT_TYPE_SIMPLE: - server_send_client(client, this->msg); - list_del_item(client->listSendMsg, i, destroyCheck); - i--; - break; - case CHECK_FRONT_TYPE_CHECK: - if (this->count == 0 || currentTime - this->time > CHECK_FRONT_SEND_TIME_INTERVAL) { - server_send_client(client, this->msg); - this->time = currentTime; - this->count++; - } - if (this->count > CHECK_FRONT_MAX_COUNT_SEND) { - id_del(this->id); - list_del_item(client->listSendMsg, i, destroyCheck); - i--; - } - break; - default: - fatal("Wrong type of the event front"); - break; - } - } -} - -void check_front_del_msg(list_t *listCheckFront, int id) -{ - int i; - - for (i = 0; i < listCheckFront->count; i++) { - checkfront_t *this; - - this = (checkfront_t *) listCheckFront->list[i]; - - if (this->id == id) { - id_del(this->id); - list_del_item(listCheckFront, i, destroyCheck); - return; - } - } -} - -void check_front_destroy(list_t *p) -{ - assert(p != NULL); - list_destroy_item(p, destroyCheck); -} diff --git a/src/base/checkFront.h b/src/base/checkFront.h deleted file mode 100644 index a65d532..0000000 --- a/src/base/checkFront.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CHECK_FRONT_H -#define CHECK_FRONT_H - -#include "list.h" -#include "server.h" - -#define CHECK_FRONT_SEND_TIME_INTERVAL 100 -#define CHECK_FRONT_MAX_COUNT_SEND 30 - -#define CHECK_FRONT_ID_NONE -1 -#define CHECK_FRONT_TYPE_SIMPLE 1 -#define CHECK_FRONT_TYPE_CHECK 2 - -extern list_t *check_front_new(); -extern void check_front_msg_add(list_t *list, char *msg, int type, int id); -extern void check_front_event(client_t *client); -extern void check_front_del_msg(list_t *listCheckFront, int id); -extern void check_front_destroy(list_t *p); - -#endif /* CHECK_FRONT_H */ diff --git a/src/base/director.c b/src/base/director.c deleted file mode 100644 index 5c005cb..0000000 --- a/src/base/director.c +++ /dev/null @@ -1,69 +0,0 @@ -#include <dirent.h> -#include <sys/types.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <unistd.h> -#include <assert.h> - -#include "main.h" -#include "list.h" - -typedef struct director_struct { - char *path; - list_t *list; -} director_t; - -director_t *director_load(char *s) -{ - director_t *new; - DIR *dir; - struct dirent *item; - char path[STR_PATH_SIZE]; - - assert(s != 0); - - new = malloc(sizeof(director_t)); - memset(new, 0, sizeof(director_t)); - - new->list = list_new(); - -#ifndef __WIN32__ - if (s[0] == '/') { -#else - if (s[1] == ':') { -#endif - strcpy(path, s); - } else { - /* this is really correct aproach */ - getcwd(path, STR_PATH_SIZE); - strcat(path, PATH_SEPARATOR); - strcat(path, s); - } - - new->path = strdup(path); - dir = opendir(new->path); - - if (dir == NULL) { - free(new->path); - free(new); - return NULL; - } - - while ((item = readdir(dir)) != NULL) { - list_add(new->list, strdup(item->d_name)); - } - - closedir(dir); - - return new; -} - -void director_destroy(director_t *p) -{ - assert(p != NULL); - - list_destroy_item(p->list, free); - free(p->path); - free(p); -} diff --git a/src/base/director.h b/src/base/director.h deleted file mode 100644 index 18c15e9..0000000 --- a/src/base/director.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef DIRECTOR_H -#define DIRECTOR_H - -#include "main.h" -#include "list.h" - -typedef struct director_struct { - char *path; - list_t *list; -} director_t; - -extern director_t *director_load(char *s); -extern void director_destroy(director_t * p); - -#endif /* DIRECTOR_H */ diff --git a/src/base/downServer.c b/src/base/downServer.c deleted file mode 100644 index a5ab58a..0000000 --- a/src/base/downServer.c +++ /dev/null @@ -1,288 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <sys/types.h> - -#include <unistd.h> -#include <fcntl.h> -#include <signal.h> -#include <sys/time.h> -#include <sys/stat.h> - -#include "main.h" -#include "list.h" -#include "server.h" -#include "myTimer.h" -#include "arena.h" -#include "serverSelect.h" -#include "downServer.h" -#include "arenaFile.h" - -#include "tcp.h" -#include "buffer.h" - -#define DOWN_SERVER_LIMIT_BUFFER 10240 -#define DOWN_SERVER_SEND_FILE_BUFFER 4096 - -#define NET_STATUS_OK 0 -#define NET_STATUS_ZOMBIE 1 - -static sock_tcp_t *sock_server_tcp; -static list_t *listClient; - -typedef struct client_down_server_struct { - int status_connect; - sock_tcp_t *sock; - buffer_t *recvBuffer; - buffer_t *sendBuffer; - - FILE *file; - int size; - int send; -} client_ds_t; - -static int getFileSize(char *s) -{ - struct stat buf; - - if (lstat(s, &buf) < 0) { - return -1; - } - - return buf.st_size; -} - -static void proto_getarena(client_ds_t *client, char *msg) -{ - arenaFile_t *arenaFile; - char msg_out[STR_PROTO_SIZE]; - char *arenaName; - - arenaName = msg + 9; - - /*printf("arena = >>%s<<\n", arenaName);*/ - arenaFile = arena_file_get_file_format_net_name(arenaName); - - if (arenaFile == NULL) { - char *error_msg = _("[Error] Arena not found\n"); - buffer_append(client->sendBuffer, error_msg, strlen(error_msg)); - return; - } - - client->file = fopen(arenaFile->path, "rb"); - - if (client->file == NULL) { - char *error_msg = _("[Error] Server has some problem\n"); - buffer_append(client->sendBuffer, error_msg, strlen(error_msg)); - return; - } - - client->size = getFileSize(arenaFile->path); - sprintf(msg_out, _("OK %d\n"), client->size); - buffer_append(client->sendBuffer, msg_out, strlen(msg_out)); -} - -static void do_proto(client_ds_t *client, char *msg) -{ - if (strncmp(msg, "GETARENA ", 9) == 0 && client->file == NULL) { - proto_getarena(client, msg); - } -} - -static client_ds_t *newClient(sock_tcp_t *sock) -{ - client_ds_t *new; - - new = malloc(sizeof(client_ds_t)); - memset(new, 0, sizeof(client_ds_t)); - - new->status_connect = NET_STATUS_OK; - new->sock = sock; - new->recvBuffer = buffer_new(DOWN_SERVER_LIMIT_BUFFER); - new->sendBuffer = buffer_new(DOWN_SERVER_LIMIT_BUFFER); - - return new; -} - -static void destroyClient(client_ds_t *client) -{ - sock_tcp_close(client->sock); - buffer_destroy(client->recvBuffer); - buffer_destroy(client->sendBuffer); - - if (client->file != NULL) { - fclose(client->file); - } - - free(client); -} - -int down_server_init(char *ip, int port) -{ - sock_server_tcp = sock_tcp_bind(ip, port); - - if (sock_server_tcp == NULL) { - error("Unable to initialize download server"); - return -1; - } - - sock_tcp_set_non_block(sock_server_tcp); - - listClient = list_new(); - - return 0; -} - -int down_server_set_select() -{ - int i; - - select_add_sock_for_read(sock_server_tcp->sock); - - for (i = 0; i < listClient->count; i++) { - client_ds_t *this; - - this = (client_ds_t *) listClient->list[i]; - - select_add_sock_for_read(this->sock->sock); - - if (buffer_get_size(this->sendBuffer) > 0) { - select_add_sock_for_write(this->sock->sock); - } - } - - return 0; -} - -static void eventNewClient(sock_tcp_t *sock_server) -{ - sock_tcp_t *sock_client; - client_ds_t *client; - - sock_client = sock_tcp_accept(sock_server); - sock_tcp_set_non_block(sock_client); - client = newClient(sock_client); - - list_add(listClient, client); -} - -static void eventReadClient(client_ds_t *client) -{ - char buffer[STR_PROTO_SIZE]; - int ret; - - memset(buffer, 0, STR_PROTO_SIZE); - - ret = sock_tcp_read(client->sock, buffer, STR_PROTO_SIZE - 1); - /*printf("sock_tcp_read = %d\n", ret);*/ - - if (ret <= 0) { - client->status_connect = NET_STATUS_ZOMBIE; - return; - } - - if (buffer_append(client->recvBuffer, buffer, ret) < 0) { - client->status_connect = NET_STATUS_ZOMBIE; - return; - } - - while (buffer_get_line(client->recvBuffer, buffer, STR_PROTO_SIZE) >= 0) { - int len; - - len = strlen(buffer); - - if (buffer[len - 1] == '\n') { - buffer[len - 1] = '\0'; - } - - do_proto(client, buffer); - /*buffer_append(client->sendBuffer, buffer, strlen(buffer));*/ - } -} - -static void eventWriteClient(client_ds_t *client) -{ - void *data; - int len; - int res; - - len = buffer_get_size(client->sendBuffer); - - if (len == 0) { - return; - } - - data = buffer_get_data(client->sendBuffer); - - res = sock_tcp_write(client->sock, data, len); - /*printf("sock_tcp_write = %d\n", res);*/ - - if (res < 0) { - client->status_connect = NET_STATUS_ZOMBIE; - return; - } - - buffer_cut(client->sendBuffer, res); -} - -static void sendFile(client_ds_t *client) -{ - char buffer[DOWN_SERVER_SEND_FILE_BUFFER]; - int count; - - count = client->size - client->send; - - if (count > DOWN_SERVER_SEND_FILE_BUFFER) { - count = DOWN_SERVER_SEND_FILE_BUFFER; - } - - if (fread(buffer, count, 1, client->file) != 1) { - client->status_connect = NET_STATUS_ZOMBIE; - return; - } - - buffer_append(client->sendBuffer, buffer, count); - client->send += count; -} - -int down_server_select_socket() -{ - int i; - - if (select_is_change_sock_for_read(sock_server_tcp->sock)) { - eventNewClient(sock_server_tcp); - } - - for (i = 0; i < listClient->count; i++) { - client_ds_t *this; - - this = (client_ds_t *) listClient->list[i]; - - if (select_is_change_sock_for_read(this->sock->sock)) { - eventReadClient(this); - } - - if (select_is_change_sock_for_write(this->sock->sock)) { - eventWriteClient(this); - } - - if (this->file != NULL && buffer_get_size(this->sendBuffer) == 0) { - sendFile(this); - } - - if (this->status_connect == NET_STATUS_ZOMBIE) { - list_del_item(listClient, i, destroyClient); - } - } - - return 0; -} - -int down_server_quit() -{ - sock_tcp_close(sock_server_tcp); - list_destroy_item(listClient, destroyClient); - - return 0; -} diff --git a/src/base/downServer.h b/src/base/downServer.h deleted file mode 100644 index cab59e3..0000000 --- a/src/base/downServer.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef DOWN_SERVER_H -#define DOWN_SERVER_H - -#include "list.h" -#include "tcp.h" -#include "buffer.h" - -extern int down_server_init(char *ip, int port); -extern int down_server_set_select(); -extern int down_server_select_socket(); -extern int down_server_quit(); - -#endif /* DOWN_SERVER_H */ diff --git a/src/base/gun.c b/src/base/gun.c deleted file mode 100644 index 9c59662..0000000 --- a/src/base/gun.c +++ /dev/null @@ -1,316 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "arena.h" -#include "tux.h" -#include "shot.h" -#include "myTimer.h" -#include "item.h" -#include "gun.h" -#include "proto.h" -#include "net_multiplayer.h" -#include "serverSendMsg.h" - -#ifndef NO_SOUND -#include "sound.h" -#endif /* NO_SOUND */ - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "world.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -static void modificiationCopuse(int courser, int right_x, int right_y, int *dest_x, int *dest_y) -{ - assert(dest_x != NULL); - assert(dest_y != NULL); - - switch (courser) { - case TUX_UP: - *dest_x = right_y; - *dest_y = -right_x; - break; - case TUX_RIGHT: - *dest_x = right_x; - *dest_y = right_y; - break; - case TUX_LEFT: - *dest_x = -right_x; - *dest_y = right_y; - break; - case TUX_DOWN: - *dest_x = right_y; - *dest_y = right_x; - break; - default: - fatal("Wrong Tux course change"); - break; - } -} - -static void addShotTrivial(tux_t *tux, int x, int y, int px, int py, int gun) -{ - int dest_x = 0, dest_y = 0; - int dest_px = 0, dest_py = 0; - shot_t *shot; - - modificiationCopuse(tux->position, px, py, &dest_px, &dest_py); - modificiationCopuse(tux->position, x, y, &dest_x, &dest_y); - - if (gun == GUN_LASSER) { - switch (tux->position) { - case TUX_UP: - dest_y -= GUN_LASSER_HORIZONTAL; - break; - case TUX_RIGHT: - break; - case TUX_LEFT: - dest_x -= GUN_LASSER_HORIZONTAL; - break; - case TUX_DOWN: - break; - } - } - - shot = shot_new(tux->x + dest_x, tux->y + dest_y, dest_px, dest_py, gun, tux->id); - space_add(arena_get_current()->spaceShot, shot); -} - -static void addShot(tux_t *tux, int x, int y, int px, int py) -{ - int gun; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - if (tux->gun == GUN_BOMBBALL) { - gun = GUN_BOMBBALL; - } else if (tux->gun == GUN_LASSER) { - gun = GUN_LASSER; - } else { - gun = GUN_SIMPLE; - } - - if (tux->bonus == BONUS_4X) { - int zal; - int i; - - zal = tux->position; - - for (i = 0; i < 4; i++) { - switch (i) { - case 0: - tux->position = TUX_UP; - break; - case 1: - tux->position = TUX_RIGHT; - break; - case 2: - tux->position = TUX_LEFT; - break; - case 3: - tux->position = TUX_DOWN; - break; - } - - addShotTrivial(tux, x, y, px, py, gun); - } - - tux->position = zal; - } else { - addShotTrivial(tux, x, y, px, py, gun); - } -} - -static void gun_shotSimpe(tux_t *tux) -{ - addShot(tux, 0, 0, +5, 0); -} - -static void gun_shotDualSimpe(tux_t *tux) -{ - addShot(tux, 0, -10, +6, 0); - addShot(tux, 0, +10, +6, 0); -} - -static void gun_shotScatter(tux_t *tux) -{ - int i; - - for (i = -2; i <= +2; i++) { - addShot(tux, 0, 0, +8, i); - } -} - -static void timer_addShotTimer(void *p) -{ - tux_t *tux; - int id; - int gun; - - id = *((int *) p); - free(p); - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux == NULL) { - return; - } - - if (tux->status != TUX_STATUS_ALIVE) { - return; - } - - gun = tux->gun; - tux->gun = GUN_SIMPLE; - addShot(tux, 0, 0, +6, 0); - tux->gun = gun; -} - -static void gun_shotTommy(tux_t *tux) -{ - int i; - - for (i = 0; i < 10; i++) { - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, - timer_addShotTimer, newInt(tux->id), i * 100); - } -} - -static void timer_addLaserTimer(void *p) -{ - tux_t *tux; - int id; - int gun; - - id = *((int *) p); - free(p); - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux == NULL) { - return; - } - - if (tux->status != TUX_STATUS_ALIVE) { - return; - } - - gun = tux->gun; - tux->gun = GUN_LASSER; - - addShot(tux, 0, 0, +10, 0); - addShot(tux, +40, 0, +10, 0); - /*addShot(tux, +40, 0, +10, 0);*/ - tux->gun = gun; -} - -static void gun_shotLasser(tux_t *tux) -{ - int i; - - for (i = 0; i < 50; i++) { - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, - timer_addLaserTimer, newInt(tux->id), i * 10); - } -} - -static void putInGunMine(tux_t *tux) -{ - int x, y; - arena_t *arena; - - x = tux->x; - y = tux->y; - - switch (tux->position) { - case TUX_UP: - y += TUX_HEIGHT; - break; - case TUX_RIGHT: - x -= TUX_WIDTH; - break; - case TUX_LEFT: - x += TUX_WIDTH; - break; - case TUX_DOWN: - y -= TUX_HEIGHT; - break; - } - - arena = arena_get_current(); - - if (arena_is_free_space(arena_get_current(), x, y, ITEM_MINE_WIDTH, ITEM_MINE_HEIGHT)) { - item_t *item; -#ifndef NO_SOUND - sound_play("put_mine", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - tux->shot[tux->gun]--; - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - item = item_new(x, y, ITEM_MINE, tux->id); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } - - space_add(arena->spaceItem, item); - } - } -} - -static void gun_shotBombball(tux_t *tux) -{ - addShot(tux, 0, 0, +10, 0); -} - -void gun_shot(tux_t *tux) -{ - if (tux->bonus != BONUS_SHOT && tux->gun != GUN_MINE) { - tux->shot[tux->gun]--; - } - - switch (tux->gun) { - case GUN_SIMPLE: - #ifndef NO_SOUND - sound_play("gun_revolver", SOUND_GROUP_BASE); - #endif /* NO_SOUND */ - gun_shotSimpe(tux); - break; - case GUN_DUAL_SIMPLE: - #ifndef NO_SOUND - sound_play("gun_revolver", SOUND_GROUP_BASE); - #endif /* NO_SOUND */ - gun_shotDualSimpe(tux); - break; - case GUN_SCATTER: - #ifndef NO_SOUND - sound_play("gun_scatter", SOUND_GROUP_BASE); - #endif /* NO_SOUND */ - gun_shotScatter(tux); - break; - case GUN_TOMMY: - #ifndef NO_SOUND - sound_play("gun_tommy", SOUND_GROUP_BASE); - #endif /* NO_SOUND */ - gun_shotTommy(tux); - break; - case GUN_LASSER: - #ifndef NO_SOUND - sound_play("gun_lasser", SOUND_GROUP_BASE); - #endif /* NO_SOUND */ - gun_shotLasser(tux); - break; - case GUN_BOMBBALL: - gun_shotBombball(tux); - break; - case GUN_MINE: - putInGunMine(tux); - break; - } -} diff --git a/src/base/gun.h b/src/base/gun.h deleted file mode 100644 index 94d2cc9..0000000 --- a/src/base/gun.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef GUN_H -#define GUN_H - -#define GUN_SHOT_WIDTH 5 -#define GUN_SHOT_HEIGHT 5 -#define GUN_LASSER_HORIZONTAL 40 -#define GUN_SHOT_VERTICAL 5 -#define GUN_BOMBBALL_WIDTH 20 -#define GUN_BOMBBALL_HEIGHT 20 - -#include "main.h" -#include "tux.h" - -extern void gun_shot(tux_t *p); - -#endif /* GUN_H */ diff --git a/src/base/homeDirector.c b/src/base/homeDirector.c deleted file mode 100644 index 5ff14b7..0000000 --- a/src/base/homeDirector.c +++ /dev/null @@ -1,44 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <unistd.h> -#include <assert.h> -#include <sys/stat.h> - -#include "main.h" -#include "homeDirector.h" - -static char homeDirector[STR_PATH_SIZE]; - -void home_director_create() -{ - char *envHome; -#ifndef __WIN32__ - envHome = getenv("HOME"); -#else /* __WIN32__ */ - envHome = getenv("USERPROFILE"); -#endif /* __WIN32__ */ - if (envHome == NULL) { - error("Environment variable HOME not found"); - exit(0); - } - - sprintf(homeDirector, "%s%s%s", envHome, PATH_SEPARATOR, HOMEDIRECTOR_NAME); - - if (access(homeDirector, F_OK) != 0) { - debug("Creating home directory [%s]", homeDirector); - -#ifndef __WIN32__ - if (mkdir(homeDirector, 0755) != 0) { -#else /* __WIN32__ */ - if (mkdir(homeDirector) != 0) { -#endif /* __WIN32__ */ - error("Unable to create home directory [%s]", homeDirector); - exit(0); - } - } -} - -char *home_director_get() -{ - return homeDirector; -} diff --git a/src/base/homeDirector.h b/src/base/homeDirector.h deleted file mode 100644 index e89f0af..0000000 --- a/src/base/homeDirector.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef HOMEDIRECTOR_H -#define HOMEDIRECTOR_H - -#include "main.h" - -#define HOMEDIRECTOR_NAME ".tuxanci" - -extern void home_director_create(); -extern char *home_director_get(); - -#endif /* HOMEDIRECTOR_H */ diff --git a/src/base/idManager.c b/src/base/idManager.c deleted file mode 100644 index 87042bb..0000000 --- a/src/base/idManager.c +++ /dev/null @@ -1,198 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "idManager.h" - -static list_t *listID; -static int lastID; - -typedef struct id_item_struct { - int id; - int count; -} id_item_t; - -static id_item_t *newIdItem(int id, int count) -{ - id_item_t *new; - - new = malloc(sizeof(id_item_t)); - new->id = id; - new->count = count; - - return new; -} - -static void destroyIdItem(id_item_t *p) -{ - assert(p != NULL); - - free(p); -} - -void id_init_list() -{ - debug("Initializing ID manager"); - - listID = list_new(); - lastID = 0; - -} - -int id_is_register(int id) -{ - int i; - - assert(listID != NULL); - - for (i = 0; i < listID->count; i++) { - id_item_t *this; - - this = listID->list[i]; - - if (this->id == id) { - return i; - } - } - - return -1; -} - -static int findNewID() -{ - int ret; - - assert(listID != NULL); - - if (listID->count >= MAX_ID - 1) { - fatal("There is no free ID left"); - } - - do { - /*ret = (random() % (listID->count + 8 )) + 1;*/ - ret = random() % MAX_ID + 1; - } while (id_is_register(ret) != -1); - - /*printf("new ID %d\n", ret);*/ - - return ret; -} - -int id_get_newcount(int count) -{ - int id; - - id = findNewID(); - - list_add(listID, newIdItem(id, count)); - - return id; -} - -int id_get_new() -{ - return id_get_newcount(1); -} - -void id_inc(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - error("Trying to increment counter of never registered ID [%d]", id); - assert(0); - return; /* ha ha ha */ - } - - this = listID->list[index]; - - this->count++; - - /*printf("inc ID %d %d\n", this->id, this->count);*/ - return; -} - -void id_del(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - error("Trying to delete never registered ID [%d]", id); - assert(0); - return; /* ha ha ha */ - } - - this = listID->list[index]; - - this->count--; - /*printf("dec ID %d %d\n", this->id, this->count);*/ - - if (this->count <= 0) { - list_del_item(listID, index, free); - /*printf("listID->count = %d\n", listID->count);*/ - } - - return; -} - -void id_replace(int old_id, int new_id) -{ - int index_old_id; - int index_new_id; - id_item_t *this; - - if (old_id == new_id) { - return; - } - - index_old_id = id_is_register(old_id); - assert(index_old_id != -1); - - index_new_id = id_is_register(new_id); - assert(index_new_id == -1); - - this = listID->list[index_old_id]; - this->id = new_id; -} - -void infoID(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - debug("Getting information of nonexistent ID [%d]", id); - - return; - } - - this = listID->list[index]; - - debug("ID information [%d]: used %d times", this->id, this->count); - - return; -} - -void id_quit_list() -{ - debug("Shutting down ID manager"); - - assert(listID != NULL); - list_destroy_item(listID, destroyIdItem); -} diff --git a/src/base/idManager.h b/src/base/idManager.h deleted file mode 100644 index 4ad2abc..0000000 --- a/src/base/idManager.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef ID_MANAGER_H -#define ID_MANAGER_H - -#include "list.h" - -#define MAX_ID 1000000 -#define ID_UNKNOWN -1 - -#ifdef __WIN32__ -#define random rand -#endif /* __WIN32__ */ - -extern void id_init_list(); -extern int id_is_register(int id); -extern int id_get_newcount(int count); -extern int id_get_new(); -extern void id_inc(int id); -extern void id_del(int id); -extern void id_replace(int old_id, int new_id); -extern void id_quit_list(); - -#endif /* ID_MANAGER_H */ diff --git a/src/base/index.c b/src/base/index.c deleted file mode 100644 index a320443..0000000 --- a/src/base/index.c +++ /dev/null @@ -1,267 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "index.h" - -#define DEBUG_INDEX - -static index_item_t *index_item_new(int key, void *data) -{ - index_item_t *new; - - new = malloc(sizeof(index_item_t)); - new->key = key; - new->data = data; - - return new; -} - -#ifdef DEBUG_INDEX -static void printIndexItem(index_item_t *p) -{ - debug("Index [%d] contains: %p", p->key, p->data); -} - -static void printListIndexItem(list_t *list) -{ - int i; - - debug("List:"); - - for (i = 0; i < list->count; i++) { - index_item_t *this; - - this = (index_item_t *) list->list[i]; - printIndexItem(this); - } -} - -static void checkList(list_t *list) -{ - int i; - int prev; - int this; - - /*return;*/ - - if (list->count == 0) { - printf(_("Nothing\n")); - return; - } - - prev = ((index_item_t *) list->list[0])->key; - - for (i = 1; i < list->count; i++) { - this = ((index_item_t *) list->list[i])->key; - - if (prev >= this) { - printListIndexItem(list); - fatal("Error in list"); - } - - prev = this; - } -} -#endif /* DEBUG_INDEX */ - -static void index_item_destroy(index_item_t *p) -{ - free(p); -} - -list_t *index_new() -{ - return list_new(); -} - -void index_add(list_t *list, int key, void *data) -{ -#ifdef DEBUG_INDEX - int count = 0; -#endif /* DEBUG_INDEX */ - - index_item_t *item; - index_item_t *this; - int min, max, point; - int len; - - item = index_item_new(key, data); - len = list->count; - - min = 0; - max = len - 1; - - for (;;) { - point = min + (max - min) / 2; - -#ifdef DEBUG_INDEX - if (++count == len * 5) { - error("Cyclic error"); - printIndexItem(item); - printListIndexItem(list); - abort(); - } -#endif /* DEBUG_INDEX */ - - if (max < 0) { - list_ins(list, 0, item); -#ifdef DEBUG_INDEX - checkList(list); -#endif /* DEBUG_INDEX */ - return; - } - - if (min >= len) { - list_add(list, item); -#ifdef DEBUG_INDEX - checkList(list); -#endif /* DEBUG_INDEX */ - return; - } - - this = (index_item_t *) list->list[point]; - -/* - printf("min = %d max = %d point = %d len = %d offset = %d\n", - min, max, point, len, offset); -*/ - - if (min > max) { - list_ins(list, point, item); -#ifdef DEBUG_INDEX - checkList(list); -#endif /* DEBUG_INDEX */ - return; - } - - if (item->key > this->key) { - min = point + 1; - continue; - } - - if (item->key < this->key) { - max = point - 1; - continue; - } - } -} - -static int getOffsetFromIndex(list_t *list, int key) -{ - index_item_t *this; - int min, max, point, len; - - len = list->count; - - if (len == 0) { - return -1; - } - - min = 0; - max = len - 1; - - for (;;) { - point = min + (max - min) / 2; - - if (max < 0 || point >= len || max < min) { - return -1; - } - - this = (index_item_t *) list->list[point]; - -/* - printf("min = %d max = %d point = %d len = %d offset = %d\n", - min, max, point, len, offset); -*/ - - if (key == this->key) { - return point; - } - - if (key > this->key) { - min = point + 1; - continue; - } - - if (key < this->key) { - max = point - 1; - continue; - } - } -} - -void *index_get(list_t *list, int key) -{ - int offset; - - offset = getOffsetFromIndex(list, key); - - if (offset != -1) { - index_item_t *this; - - this = list->list[offset]; - return this->data; - } - - return NULL; -} - -void index_del(list_t *list, int key) -{ - int offset; - - offset = getOffsetFromIndex(list, key); - - if (offset != -1) { - list_del_item(list, offset, index_item_destroy); - } -} - -void index_del_with_object(list_t *list, int key, void *f) -{ - int offset; - - offset = getOffsetFromIndex(list, key); - - if (offset != -1) { - index_item_t *this; - void (*fce) (void *p); - - this = list->list[offset]; - - fce = f; - fce(this); - - list_del_item(list, offset, index_item_destroy); - } -} - -void index_action(list_t *list, void *f) -{ - int i; - - for (i = 0; i < list->count; i++) { - index_item_t *this; - void (*fce) (void *p); - - this = list->list[i]; - - fce = f; - fce(this); - } -} - -void index_destroy(list_t *list) -{ - list_destroy_item(list, index_item_destroy); -} - -void index_destroyWithObject(list_t *list, void *f) -{ - index_action(list, f); - list_destroy_item(list, index_item_destroy); -} diff --git a/src/base/index.h b/src/base/index.h deleted file mode 100644 index 4bb1c1b..0000000 --- a/src/base/index.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef INDEX_H -#define INDEX_H - -#include "main.h" -#include "list.h" - -typedef struct index_item_struct { - int key; - void *data; -} index_item_t; - -extern list_t *index_new(); -extern void index_add(list_t *list, int key, void *data); -extern void *index_get(list_t *list, int key); -extern void index_del(list_t *list, int key); -extern void index_del_with_object(list_t *list, int key, void *f); -extern void index_action(list_t *list, void *f); -extern void index_destroy(list_t *list); -extern void index_destroyWithObject(list_t *list, void *f); - -#endif /* INDEX_H */ diff --git a/src/base/item.c b/src/base/item.c deleted file mode 100644 index 66d69e2..0000000 --- a/src/base/item.c +++ /dev/null @@ -1,551 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "arena.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "proto.h" -#include "idManager.h" -#include "net_multiplayer.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#include "radar.h" -#include "layer.h" - -#include "world.h" -#include "setting.h" - -#ifndef NO_SOUND -#include "sound.h" -#endif /* NO_SOUND */ - -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -#ifndef PUBLIC_SERVER -static image_t *g_item[ITEM_COUNT]; -#endif /* PUBLIC_SERVER */ - -static bool_t isItemInit = FALSE; - -bool_t item_is_inicialized() -{ - return isItemInit; -} - -void item_init() -{ -#ifndef PUBLIC_SERVER - g_item[GUN_DUAL_SIMPLE] = image_add("item.dual.png", IMAGE_ALPHA, "item_dual_simple", IMAGE_GROUP_BASE); - g_item[GUN_TOMMY] = image_add("item.tommy.png", IMAGE_ALPHA, "item_tommy_simple", IMAGE_GROUP_BASE); - g_item[GUN_SCATTER] = image_add("item.scatter.png", IMAGE_ALPHA, "item_scatter_simple", IMAGE_GROUP_BASE); - g_item[GUN_LASSER] = image_add("item.lasser.png", IMAGE_ALPHA, "item_lasser_simple", IMAGE_GROUP_BASE); - g_item[GUN_MINE] = image_add("item.mine.png", IMAGE_ALPHA, "item_mines_simple", IMAGE_GROUP_BASE); - g_item[GUN_BOMBBALL] = image_add("item.bombball.png", IMAGE_ALPHA, "item_bombball_simple", IMAGE_GROUP_BASE); - g_item[ITEM_MINE] = image_add("mine.png", IMAGE_ALPHA, "item_mine_simple", IMAGE_GROUP_BASE); - g_item[ITEM_EXPLOSION] = image_add("explosion.png", IMAGE_ALPHA, "item_explosion_simple", IMAGE_GROUP_BASE); - g_item[ITEM_BIG_EXPLOSION] = image_add("big-explosion.png", IMAGE_ALPHA, "item_big-explosion_simple", IMAGE_GROUP_BASE); - g_item[BONUS_SPEED] = image_add("item.bonus.speed.png", IMAGE_ALPHA, "item_bonus_speed", IMAGE_GROUP_BASE); - g_item[BONUS_SHOT] = image_add("item.bonus.shot.png", IMAGE_ALPHA, "item_shot_speed", IMAGE_GROUP_BASE); - g_item[BONUS_TELEPORT] = image_add("item.bonus.teleport.png", IMAGE_ALPHA, "item_teleport_speed", IMAGE_GROUP_BASE); - g_item[BONUS_GHOST] = image_add("item.bonus.ghost.png", IMAGE_ALPHA, "item_ghost_speed", IMAGE_GROUP_BASE); - g_item[BONUS_4X] = image_add("item.bonus.4x.png", IMAGE_ALPHA, "item_4x_speed", IMAGE_GROUP_BASE); - g_item[BONUS_HIDDEN] = image_add("item.bonus.hidden.png", IMAGE_ALPHA, "item_hidden_speed", IMAGE_GROUP_BASE); -#endif /* PUBLIC_SERVER */ - isItemInit = TRUE; -} - -item_t *item_new(int x, int y, int type, int author_id) -{ - item_t *new; - - new = malloc(sizeof(item_t)); - assert(new != NULL); - new->type = type; - new->id = id_get_new(); - new->x = x; - new->y = y; - new->frame = 0; - new->count = 0; -#ifndef PUBLIC_SERVER - new->img = g_item[type]; -#endif /* PUBLIC_SERVER */ - new->author_id = author_id; - - switch (type) { - case GUN_DUAL_SIMPLE: - case GUN_SCATTER: - case GUN_TOMMY: - case GUN_LASSER: - case GUN_MINE: - case GUN_BOMBBALL: - new->w = ITEM_GUN_WIDTH; - new->h = ITEM_GUN_HEIGHT; - break; - case ITEM_MINE: - new->w = ITEM_MINE_WIDTH; - new->h = ITEM_MINE_HEIGHT; - break; - case ITEM_EXPLOSION: -#ifndef NO_SOUND - sound_play("explozion", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - new->w = ITEM_EXPLOSION_WIDTH; - new->h = ITEM_EXPLOSION_HEIGHT; - break; - case ITEM_BIG_EXPLOSION: -#ifndef NO_SOUND - sound_play("explozion", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - new->w = ITEM_BIG_EXPLOSION_WIDTH; - new->h = ITEM_BIG_EXPLOSION_HEIGHT; - break; - case BONUS_SPEED: - case BONUS_SHOT: - case BONUS_TELEPORT: - case BONUS_GHOST: - case BONUS_4X: - case BONUS_HIDDEN: - new->w = ITEM_BONUS_WIDTH; - new->h = ITEM_BONUS_HEIGHT; - break; - } - - return new; -} - -void item_get_status(void *p, int *id, int *x, int *y, int *w, int *h) -{ - item_t *item; - - item = p; - *id = item->id; - *x = item->x; - *y = item->y; - *w = item->w; - *h = item->h; -} - -void item_set_status(void *p, int x, int y, int w, int h) -{ - item_t *item; - - item = p; - item->x = x; - item->y = y; - item->w = w; - item->h = h; -} - -void item_replace_id(item_t *item, int id) -{ - id_replace(item->id, id); - item->id = id; -} - -#ifdef PUBLIC_SERVER -static void action_countitem(space_t *space, item_t *item, int *count) -{ - if ((item->type >= GUN_DUAL_SIMPLE && item->type <= GUN_BOMBBALL) || - (item->type >= BONUS_SPEED && item->type <= BONUS_HIDDEN)) { - (*count)++; - } -} - -static int getCountWeaponOrBonusItem(space_t *spaceItem) -{ - int count = 0; - - space_action(spaceItem, action_countitem, &count); - - return count; -} -#endif /* PUBLIC_SERVER */ - -void item_add_new_item(space_t *spaceItem, int author_id) -{ - arena_t *arena; - item_t *item; - int new_x, new_y; - int type; - -#ifdef PUBLIC_SERVER - if (getCountWeaponOrBonusItem(spaceItem) >= atoi(public_server_get_setting("MAX_ITEM", "--max-item", "100"))) { - return; - } -#endif /* PUBLIC_SERVER */ - -#ifndef PUBLIC_SERVER - if (setting_is_any_item() == FALSE || net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } -#endif /* PUBLIC_SERVER */ - - arena = arena_get_current(); - arena_find_free_space(arena_get_current(), &new_x, &new_y, ITEM_GUN_WIDTH, ITEM_GUN_HEIGHT); - - type = GUN_DUAL_SIMPLE; - -#ifndef PUBLIC_SERVER - do { -#endif /* PUBLIC_SERVER */ - switch (random() % 12) { - case 0: - type = GUN_DUAL_SIMPLE; - break; - case 1: - type = GUN_SCATTER; - break; - case 2: - type = GUN_TOMMY; - break; - case 3: - type = GUN_MINE; - break; - case 4: - type = GUN_LASSER; - break; - case 5: - type = GUN_BOMBBALL; - break; - case 6: - type = BONUS_SPEED; - break; - case 7: - type = BONUS_SHOT; - break; - case 8: - type = BONUS_TELEPORT; - break; - case 9: - type = BONUS_GHOST; - break; - case 10: - type = BONUS_4X; - break; - case 11: - type = BONUS_HIDDEN; - break; - } -#ifndef PUBLIC_SERVER - } while (setting_is_item(type) == FALSE || - (net_multiplayer_get_game_type() == NET_GAME_TYPE_NONE && type == BONUS_HIDDEN)); -#endif /* PUBLIC_SERVER */ - item = item_new(new_x, new_y, type, author_id); - space_add(spaceItem, item); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } - -#ifndef PUBLIC_SERVER - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - radar_add(item->id, new_x, new_y, RADAR_TYPE_ITEM); - } -#endif /* PUBLIC_SERVER */ -} - -#ifndef PUBLIC_SERVER -void item_draw(item_t *p) -{ - assert(p != NULL); - addLayer(p->img, p->x, p->y, p->frame * p->w, 0, p->w, p->h, TUX_LAYER); -} - -void item_draw_list(list_t *listItem) -{ - item_t *thisItem; - int i; - - assert(listItem != NULL); - - for (i = 0; i < listItem->count; i++) { - thisItem = (item_t *) listItem->list[i]; - assert(thisItem != NULL); - item_draw(thisItem); - } -} - -#endif /* PUBLIC_SERVER */ - -static void action_itemevent(space_t *space, item_t *item, void *p) -{ - my_time_t currentTime; - - currentTime = timer_get_current_time(); - item->count++; - - if (item->count == ITEM_MAX_COUNT) { - item->count = 0; - item->frame++; - } - - switch (item->type) { - case GUN_TOMMY: - case GUN_DUAL_SIMPLE: - case GUN_SCATTER: - case GUN_LASSER: - case GUN_MINE: - case GUN_BOMBBALL: - if (item->frame == ITEM_GUN_MAX_FRAME) { - item->frame = 0; - } - break; - case ITEM_MINE: - if (item->frame == ITEM_MINE_MAX_FRAME) { - item->frame = 0; - } - break; - case ITEM_EXPLOSION: - case ITEM_BIG_EXPLOSION: - if (item->frame == ITEM_EXPLOSION_MAX_FRAME) { - space_del_with_item(space, item, item_destroy); - } - break; - case BONUS_SPEED: - case BONUS_SHOT: - case BONUS_TELEPORT: - case BONUS_GHOST: - case BONUS_4X: - case BONUS_HIDDEN: - if (item->frame == ITEM_GUN_MAX_FRAME) { - item->frame = 0; - } - break; - } -} - -void item_event(space_t *spaceItem) -{ - space_action(spaceItem, action_itemevent, NULL); -} - -static void mineExplosion(space_t *spaceItem, item_t *item) -{ - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - int x, y; - item_t *item_explosion; - - x = (item->x + item->w / 2) - ITEM_BIG_EXPLOSION_WIDTH / 2; - y = (item->y + item->h / 2) - ITEM_BIG_EXPLOSION_HEIGHT / 2; - item_explosion = item_new(x, y, ITEM_BIG_EXPLOSION, item->author_id); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_additem_server(PROTO_SEND_ALL, NULL, item_explosion); - } - - space_add(spaceItem, item_explosion); - } - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - space_del_with_item(spaceItem, item, item_destroy); - } -} - -static void action_item(space_t *space, item_t *item, int *isDel) -{ - switch (item->type) { - case ITEM_MINE: - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - mineExplosion(space, item); - } - break; - case ITEM_EXPLOSION: - case ITEM_BIG_EXPLOSION: - *isDel = 1; - break; - } -} - -static void action_shot(space_t *space, shot_t *shot, space_t *spaceItem) -{ - int isDel = 0; - - space_action_from_location(spaceItem, action_item, &isDel, - shot->x, shot->y, shot->w, shot->h); - - if (isDel) { - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); - } - - space_del_with_item(space, shot, shot_destroy); - } -} - -void item_event_conglicts_shot_with_item(arena_t *arena) -{ - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - space_action(arena->spaceShot, action_shot, arena->spaceItem); -} - -static void tux_event_tux_is_deadWithItem(tux_t *tux, item_t *item) -{ - if (tux->bonus == BONUS_GHOST) { - return; - } - - if (tux->bonus == BONUS_TELEPORT) { - tux_teleport(tux); - return; - } - - if (item->author_id == tux->id) { - tux->score--; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - - if (item->author_id != tux->id) { - tux_t *author; - - author = space_get_object_id(arena_get_current()->spaceTux, item->author_id); - - if (author != NULL) { - author->score++; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); - } - } - } - - tux_event_tux_is_dead(tux); -} - -static void tuxGiveBonus(tux_t *tux, item_t *item) -{ -#ifndef NO_SOUND - sound_play("item_bonus", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - tux->bonus = item->type; - tux->bonus_time = TUX_MAX_BONUS; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - -#ifndef PUBLIC_SERVER - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - radar_del(item->id); - } -#endif /* PUBLIC_SERVER */ -} - -static void tuxGiveGun(tux_t *tux, item_t *item) -{ -#ifndef NO_SOUND - sound_play("item_gun", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - tux->pickup_time = 0; - tux->shot[item->type] += GUN_MAX_SHOT; - tux->gun = item->type; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - -#ifndef PUBLIC_SERVER - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - radar_del(item->id); - } -#endif /* PUBLIC_SERVER */ -} - -static void action_giveitem(space_t *space, item_t *item, tux_t *tux) -{ - switch (item->type) { - case GUN_TOMMY: - case GUN_DUAL_SIMPLE: - case GUN_SCATTER: - case GUN_LASSER: - case GUN_MINE: - case GUN_BOMBBALL: - tuxGiveGun(tux, item); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - space_del_with_item(space, item, item_destroy); -#ifdef PUBLIC_SERVER - item_add_new_item(space, ID_UNKNOWN); -#endif /* PUBLIC_SERVER */ - break; - - case ITEM_MINE: - if (tux->bonus != BONUS_GHOST) { - mineExplosion(space, item); - } - break; - case ITEM_EXPLOSION: - case ITEM_BIG_EXPLOSION: - if (tux->bonus != BONUS_GHOST) { - tux_event_tux_is_deadWithItem(tux, item); - } - break; - case BONUS_SPEED: - case BONUS_SHOT: - case BONUS_TELEPORT: - case BONUS_GHOST: - case BONUS_4X: - case BONUS_HIDDEN: - tuxGiveBonus(tux, item); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, item->id); - } - - space_del_with_item(space, item, item_destroy); -#ifdef PUBLIC_SERVER - item_add_new_item(space, ID_UNKNOWN); -#endif /* PUBLIC_SERVER */ - break; - } -} - -void item_tux_give_list_item(tux_t *tux, space_t *spaceItem) -{ - int x, y, w, h; - - assert(spaceItem != NULL); - assert(tux != NULL); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - if (tux->status == TUX_STATUS_DEAD) { - return; - } - - tux_get_proportion(tux, &x, &y, &w, &h); - space_action_from_location(spaceItem, action_giveitem, tux, x, y, w, h); -} - -void item_destroy(item_t *p) -{ - assert(p != NULL); - id_del(p->id); - free(p); -} - -void item_quiy() -{ - isItemInit = FALSE; -} diff --git a/src/base/item.h b/src/base/item.h deleted file mode 100644 index 48f2930..0000000 --- a/src/base/item.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef ITEM_H -#define ITEM_H - -#include "main.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#endif /* PUBLIC_SERVER */ - -#include "list.h" -#include "tux.h" -#include "myTimer.h" - -#define ITEM_SYNC_TIMEOUT 5000 - -#ifdef __WIN32__ -#define random rand -#endif /* __WIN32__ */ - -#define ITEM_MAX_COUNT 1 -#define ITEM_GUN_MAX_FRAME 8 -#define ITEM_MINE_MAX_FRAME 1 -#define ITEM_EXPLOSION_MAX_FRAME 10 -#define ITEM_BONUS_MAX_FRAME 8 -#define ITEM_GUN_WIDTH 50 -#define ITEM_GUN_HEIGHT 20 -#define ITEM_MINE_WIDTH 15 -#define ITEM_MINE_HEIGHT 11 -#define ITEM_EXPLOSION_WIDTH 40 -#define ITEM_EXPLOSION_HEIGHT 40 -#define ITEM_BIG_EXPLOSION_WIDTH 100 -#define ITEM_BIG_EXPLOSION_HEIGHT 100 -#define ITEM_BONUS_WIDTH 20 -#define ITEM_BONUS_HEIGHT 20 - -typedef struct item_struct { - int id; - int type; /* type of the item */ - - /* position */ - int x; - int y; - - /* size */ - int w; - int h; - - int frame; /* number of animation frames */ - int count; - - int author_id; -#ifndef PUBLIC_SERVER - image_t *img; /* picture of the item*/ -#endif -} item_t; - -extern bool_t item_is_inicialized(); -extern void item_init(); -extern item_t *item_new(int x, int y, int type, int author_id); -extern void item_get_status(void *p, int *id, int *x, int *y, int *w, int *h); -extern void item_set_status(void *p, int x, int y, int w, int h); -extern void item_replace_id(item_t *item, int id); -extern void item_add_new_item(space_t *spaceItem, int author_id); -#ifndef PUBLIC_SERVER -extern void item_draw(item_t *p); -extern void item_draw_list(list_t *listItem); -#endif /* PUBLIC_SERVER */ -extern void item_event(space_t *listSpace); -extern void item_event_conglicts_shot_with_item(arena_t *arena); -extern void item_event_tux_give_item(tux_t *tux, item_t *item, space_t *spaceItem); -extern void item_tux_give_list_item(tux_t *tux, space_t *spaceItem); -extern void item_destroy(item_t *p); -extern void item_quiy(); - -#endif /* ITEM_H */ diff --git a/src/base/list.c b/src/base/list.c deleted file mode 100644 index 482b50e..0000000 --- a/src/base/list.c +++ /dev/null @@ -1,184 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" - -list_t *list_new() -{ - list_t *new; - - new = malloc(sizeof(list_t)); - memset(new, 0, sizeof(list_t)); - - return new; -} - -list_t *list_clone(list_t *p) -{ - list_t *new; - - assert(p != NULL); - new = malloc(sizeof(list_t)); - memcpy(new, p, sizeof(list_t)); - - if (p->list != NULL) { - new->list = malloc(p->alloc * sizeof(void *)); - memcpy(new->list, p->list, p->alloc * sizeof(void *)); - } - - return new; -} - -list_t *list_clone_item(list_t *p, void *f) -{ - list_t *new; - void *(*fce) (void *); - int i; - - assert(p != NULL); - assert(f != NULL); - new = list_clone(p); - fce = f; - - for (i = 0; i < p->count; i++) { - new->list[i] = fce(p->list[i]); - } - - return new; -} - -void list_add(list_t *p, void *item) -{ - assert(p != NULL); - - if (p->alloc == 0) { - p->alloc = LIST_ALLOC_LIMIT; - p->count = 1; - p->list = malloc(p->alloc * sizeof(void *)); - p->list[0] = item; - return; - } - - if (p->count + 1 <= p->alloc) { - p->list[p->count] = item; - p->count += 1; - return; - } - - if (p->count + 1 > p->alloc) { -#ifdef DEBUG_LIST - printf("[Debug] Reallocating list from %d to %d (count: %d)\n", - p->alloc, p->alloc + LIST_ALLOC_LIMIT, p->count); -#endif - p->alloc += LIST_ALLOC_LIMIT; - p->list = realloc(p->list, p->alloc * sizeof(void *)); - p->list[p->count] = item; - p->count++; - return; - } -} - -void list_ins(list_t *p, int n, void *item) -{ - assert(p != NULL); - list_add(p, NULL); /* :) */ - assert(n >= 0 || n < p->count); - memmove(p->list + n + 1, p->list + n, ((p->count - 1) - n) * sizeof(void *)); - p->list[n] = item; -} - -void *list_get(list_t *p, int n) -{ - assert(p != NULL); - assert(n >= 0 || n < p->count); - return p->list[n]; -} - -int list_search(list_t *p, void *n) -{ - int i; - - assert(p != NULL); - - for (i = 0; i < p->count; i++) { - if (p->list[i] == n) { - return i; - } - } - - return -1; -} - -void list_del(list_t *p, int n) -{ - assert(p != NULL); - assert(n >= 0 || n < p->count); - - memmove(p->list + n, p->list + n + 1, ((p->count - 1) - n) * sizeof(void *)); - p->count--; - - if (p->count + LIST_ALLOC_LIMIT < p->alloc) { - void **new; - -#ifdef DEBUG_LIST - printf("Reallocating list from %d to %d (count: %d)\n", - p->alloc, p->alloc - LIST_ALLOC_LIMIT, p->count); -#endif - p->alloc -= LIST_ALLOC_LIMIT; - new = malloc(p->alloc * sizeof(void *)); - memcpy(new, p->list, p->count * sizeof(void *)); - free(p->list); - p->list = new; - } -} - -void list_del_item(list_t *p, int n, void *f) -{ - int (*fce) (void *); - - assert(p != NULL); - assert(n >= 0 || n < p->count); - fce = f; - - if (fce != NULL) { - fce(p->list[n]); - } - - list_del(p, n); -} - -void list_do_empty(list_t *p) -{ - p->count = 0; -} - -void list_destroy(list_t *p) -{ - assert(p != NULL); - - if (p->list != NULL) { - free(p->list); - } - - free(p); -} - -void list_destroy_item(list_t *p, void *f) -{ - void (*fce) (void *); - int i; - - assert(p != NULL); - assert(f != NULL); - - fce = f; - - for (i = 0; i < p->count; i++) { - fce(p->list[i]); - } - - list_destroy(p); -} diff --git a/src/base/list.h b/src/base/list.h deleted file mode 100644 index 7c9c610..0000000 --- a/src/base/list.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef LIST_H -#define LIST_H - -#include "main.h" - -#define LIST_ALLOC_LIMIT 16 - -typedef struct list_str { - void **list; - int count; - int alloc; -} list_t; - -extern list_t *list_new(); -extern list_t *list_clone(list_t *p); -extern list_t *list_clone_item(list_t *p, void *f); -extern void list_add(list_t *p, void *item); -extern void list_ins(list_t *p, int n, void *item); -extern void *list_get(list_t *p, int n); -extern int list_search(list_t *p, void *n); -extern void list_del(list_t *p, int n); -extern void list_del_item(list_t *p, int n, void *f); -extern void list_do_empty(list_t *p); -extern void list_destroy(list_t *p); -extern void list_destroy_item(list_t *p, void *f); - -#endif /* LIST_H */ diff --git a/src/base/main.c b/src/base/main.c deleted file mode 100644 index b2ab1f6..0000000 --- a/src/base/main.c +++ /dev/null @@ -1,186 +0,0 @@ -#include <time.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <assert.h> -#include <sys/stat.h> - -#ifdef __WIN32__ -#include <windows.h> -#include <wininet.h> -#endif /* __WIN32__ */ - -#include "main.h" -#include "tux.h" - -#ifndef PUBLIC_SERVER -#include "game.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -static int my_argc; -static char **my_argv; - -char *getParam(char *s) -{ - unsigned int len; - int i; - - len = strlen(s); - - for (i = 1; i < my_argc; i++) { - /*printf("%s %s\n", s, my_argv[i]);*/ - - if (strlen(my_argv[i]) < len) { - continue; - } - - if (strncmp(s, my_argv[i], len) == 0 && strchr(my_argv[i], '=') != NULL ) { - return strchr(my_argv[i], '=') + 1; - } - } - - return NULL; -} - -char *getParamElse(char *s1, char *s2) -{ - char *ret; - ret = getParam(s1); - - if (ret == NULL) { - return s2; - } - - return ret; -} - -bool_t isParamFlag(char *s) -{ - int i; - - for (i = 0; i < my_argc; i++) { - if (strcmp(s, my_argv[i]) == 0) { - return TRUE; - } - } - - return FALSE; -} - -char *get_program_name() -{ - return my_argv[0]; -} - -char *getString(int n) -{ - char str[STR_NUM_SIZE]; - sprintf(str, "%d", n); - return strdup(str); -} - -char *get_string_static(int n) -{ - static char str[STR_SIZE]; - sprintf(str, "%d", n); - - return str; /* !!! */ -} - -int *newInt(int x) -{ - int *new; - - new = malloc(sizeof(int)); - *new = x; - - return new; -} - -void accessExistFile(const char *s) -{ - if (access(s, F_OK) != 0) { - error("File not found [%s]", s); - error("Shutting down the game"); - exit(-1); - } -} - -int tryExistFile(const char *s) -{ - if (access(s, F_OK) != 0) { - return 1; - } else { - return 0; - } -} - -int isFillPath(const char *path) -{ - assert(path != NULL); - -#ifndef __WIN32__ - if (path[0] == '/') { /* for Unix-like systems ;) */ -#else /* __WIN32__ */ - if (path[1] == ':') { /* for Windows-like systems ;) */ -#endif /* __WIN32__ */ - return 1; - } - - return 0; -} - -#ifdef __WIN32__ -int WINAPI -WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -#else /* __WIN32__ */ -int main(int argc, char *argv[]) -#endif /* __WIN32__ */ -{ -#ifndef __WIN32__ - signal(SIGPIPE, SIG_IGN); -#endif /* __WIN32__ */ - - srand((unsigned) time(NULL)); - -#ifdef NLS - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, PATH_LOCALE); - bind_textdomain_codeset(PACKAGE, "UTF-8"); - textdomain(PACKAGE); -#endif /* NLS */ - -#ifndef __WIN32__ - my_argc = argc; - my_argv = argv; -#endif /* __WIN32__ */ - -/* - test_space(); - exit(0); -*/ - -#ifdef __WIN32__ - WORD wVersionRequested = MAKEWORD(1, 1); /* WinSock version */ - WSADATA data; /* WinSock information structure */ - - /* let's initialize WinSock */ - if (WSAStartup(wVersionRequested, &data) != 0) { - error("Initialization of WinSock failed"); - error("Shutting down the game"); - exit(-1); - } -#endif /* __WIN32__ */ - -#ifndef PUBLIC_SERVER - game_start(); -#else /* PUBLIC_SERVER */ - public_server_start(); -#endif /* PUBLIC_SERVER */ - - return 0; -} diff --git a/src/base/main.h b/src/base/main.h deleted file mode 100644 index cf380cd..0000000 --- a/src/base/main.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef MAIN_H -#define MAIN_H - -#ifdef NLS -#include <libintl.h> -#include <locale.h> -#endif /* NLS */ - -#include "bool.h" -#include "string_length.h" -#include "path.h" - -#define WINDOW_SIZE_X 800 -#define WINDOW_SIZE_Y 600 - -#ifdef NLS -#define _(text) gettext(text) -#else /* NLS */ -#define _(text) (text) -#endif /* NLS */ - -#include "msg.h" - -#ifndef __WIN32__ -#define PATH_SEPARATOR "/" -#else /* __WIN32__ */ -#define PATH_SEPARATOR "\\" -#endif /* __WIN32__ */ - -extern char *getParam(char *s); -extern char *getParamElse(char *s1, char *s2); -extern bool_t isParamFlag(char *s); -extern char *get_program_name(); -extern char *getString(int n); -extern char *get_string_static(int n); -extern int *newInt(int x); -extern int isFillPath(const char *path); -extern void accessExistFile(const char *s); -extern int tryExistFile(const char *s); - -#endif /* MAIN_H */ diff --git a/src/base/modules.c b/src/base/modules.c deleted file mode 100644 index eab4506..0000000 --- a/src/base/modules.c +++ /dev/null @@ -1,317 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "shot.h" -#include "modules.h" -#include "arena.h" -#include "arenaFile.h" -#include "proto.h" -#include "shareFunction.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#include "layer.h" -#include "configFile.h" -#endif /* PUBLIC_SERVER */ - -static export_fce_t export_fce = { -#ifndef PUBLIC_SERVER - .fce_addLayer = addLayer, - .fce_image_get = image_get, -#endif /* PUBLIC_SERVER */ - .fce_module_load_dep = module_load_dep, - .fce_share_function_add = share_function_add, - .fce_share_function_get = share_function_get, - - .fce_getValue = arena_file_get_value, - .fce_net_multiplayer_get_game_type = net_multiplayer_get_game_type, - .fce_tux_get_proportion = tux_get_proportion, - .fce_tux_set_proportion = tux_set_proportion, - .fce_tux_action = tux_action, - - .fce_timer_get_current_time = timer_get_current_time, - - .fce_arena_get_current = arena_get_current, - .fce_arena_conflict_space = arena_conflict_space, - .fce_arena_is_free_space = arena_is_free_space, - .fce_arena_find_free_space = arena_find_free_space, - - .fce_proto_send_module_server = proto_send_module_server, - .fce_proto_send_del_server = proto_send_del_server, -#ifndef PUBLIC_SERVER - .fce_proto_send_module_client = proto_send_module_client, -#endif /* PUBLIC_SERVER */ - .fce_shot_destroy = shot_destroy, - .fce_shot_bound_bombBall = shot_bound_bombBall, - .fce_shot_transform_lasser = shot_transform_lasser -}; - -static list_t *listModule; -static mod_reg_t mod_reglist; - -/* register module as valid */ -static mod_reg_t *mod_register(char *name, mod_sym_t *sym) -{ - /* alloc and init context */ - mod_reg_t *mod = (mod_reg_t *) malloc(sizeof(mod_reg_t)); - - if (!mod) { - return 0; - } - - mod->name = strdup(name); - mod->sym = sym; - - /* add entry into list */ - mod->next = &mod_reglist; - mod->prev = mod_reglist.prev; - mod->prev->next = mod; - mod->next->prev = mod; - - return mod; -} - -/* free all unnecessary memory blocks from module's reglist */ -static void mod_destroy() -{ - unsigned i; - for (;;) { - i = 0; - - mod_reg_t *mod; - for (mod = mod_reglist.next; mod != &mod_reglist; mod = mod->next) { - /* delete entry from context */ - mod->next->prev = mod->prev; - mod->prev->next = mod->next; - - free(mod->name); - free(mod); - - i++; - - break; - } - - if (!i) { - break; - } - } -} - -static mod_reg_t *mod_find(char *name) -{ - mod_reg_t *mod; - for (mod = mod_reglist.next; mod != &mod_reglist; mod = mod->next) { - if (!strcmp(mod->name, name)) { - return mod; - } - } - - return 0; -} - -static module_t *newModule(char *name) -{ - assert(name != NULL); - - mod_reg_t *mod = mod_find(name); - - if (!mod) { - error("Module [%s] is unavailable", name); - return 0; - } - - module_t *ret = malloc(sizeof(module_t)); - - if (!ret) { - return 0; - } - - ret->name = strdup(name); - - if (!ret->name) { - free(ret); - return 0; - } - - ret->fce_init = mod->sym->init; -#ifndef PUBLIC_SERVER - ret->fce_draw = mod->sym->draw; -#endif /* PUBLIC_SERVER */ - ret->fce_event = mod->sym->event; - ret->fce_destroy = mod->sym->destroy; - ret->fce_isConflict = mod->sym->isConflict; - ret->fce_cmd = mod->sym->cmdArena; - ret->fce_recvMsg = mod->sym->recvMsg; - - debug("Loading module [%s]", name); - - if (ret->fce_init(&export_fce) != 0) { - error("Unable to load module [%s]", name); - free(ret->name); - free(ret); - return NULL; - } - - return ret; -} - -static int destroyModule(module_t *p) -{ - assert(p != NULL); - - debug("Unloading module [%s]", p->name); - - p->fce_destroy(); - - free(p->name); - free(p); - - return 0; -} - -void module_init() -{ - listModule = list_new(); - share_function_init(); - - mod_reglist.next = &mod_reglist; - mod_reglist.prev = &mod_reglist; - - mod_register("libmodAI", &modai_sym); - mod_register("libmodWall", &modwall_sym); - mod_register("libmodPipe", &modpipe_sym); - mod_register("libmodMove", &modmove_sym); - mod_register("libmodBasic", &modbasic_sym); - mod_register("libmodTeleport", &modteleport_sym); -} - -int isModuleLoaded(char *name) -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - - if (strcmp(this->name, name) == 0) { - return 1; - } - } - - return 0; -} - -int module_load(char *name) -{ - module_t *module; - - if (isModuleLoaded(name)) { - error("Unable to load already loaded module [%s]", name); - return -1; - } - - module = newModule(name); - - if (module == NULL) { - error("Unable to load module [%s]", name); - return -1; - } - - list_add(listModule, module); - - return 0; -} - -int module_load_dep(char *name) -{ - if (isModuleLoaded(name)) { - return 0; - } - - return module_load(name); -} - -void module_cmd(char *s) -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - this->fce_cmd(s); - } -} - -#ifndef PUBLIC_SERVER -void module_draw(int x, int y, int w, int h) -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - this->fce_draw(x, y, w, h); - } -} -#endif /* PUBLIC_SERVER */ - -void module_event() -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - this->fce_event(); - } -} - -int module_is_conflict(int x, int y, int w, int h) -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - - if (this->fce_isConflict(x, y, w, h) == 1) { - return 1; - } - } - - return 0; -} - -int module_recv_msg(char *msg) -{ - int i; - - for (i = 0; i < listModule->count; i++) { - module_t *this; - - this = (module_t *) listModule->list[i]; - this->fce_recvMsg(msg); - } - - return 0; -} - -void module_quit() -{ - list_destroy_item(listModule, destroyModule); - share_function_quit(); - - mod_destroy(); -} diff --git a/src/base/modules.h b/src/base/modules.h deleted file mode 100644 index 7cece8a..0000000 --- a/src/base/modules.h +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef MODULE_H -#define MODULE_H - -#include "tux.h" -#include "shot.h" -#include "arena.h" -#include "proto.h" -#include "myTimer.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#endif /* PUBLIC_SERVER */ - -typedef struct export_fce_s { - int (*fce_getValue) (char *line, char *env, char *val, int len); - -#ifndef PUBLIC_SERVER - image_t *(*fce_image_get) (char *group, char *name); - - void (*fce_addLayer) (image_t *img, int x, int y, int px, int py, - int w, int h, int player); -#endif - - int (*fce_net_multiplayer_get_game_type) (); - int (*fce_module_load_dep) (char *name); - void (*fce_share_function_add) (char *name, void *function); - void *(*fce_share_function_get) (char *name); - - void (*fce_tux_get_proportion) (tux_t *tux, int *x, int *y, int *w, int *h); - void (*fce_tux_set_proportion) (tux_t *tux, int x, int y); - tux_t *(*fce_getTuxID) (list_t *listTux, int id); - void (*fce_tux_action) (tux_t *tux, int action); - - arena_t *(*fce_arena_get_current) (); - int (*fce_arena_conflict_space) (int x1, int y1, int w1, int h1, - int x2, int y2, int w2, int h2); - int (*fce_arena_is_free_space) (arena_t *arena, int x, int y, int w, int h); - void (*fce_arena_find_free_space) (arena_t *arena, int *x, int *y, int w, int h); - - void (*fce_proto_send_del_server)(int type, client_t *client, int id); - void (*fce_proto_send_module_server) (int type, client_t *client, char *msg); - void (*fce_proto_send_module_client) (char *msg); - - my_time_t(*fce_timer_get_current_time) (); - - void (*fce_shot_destroy) (shot_t *p); - void (*fce_shot_bound_bombBall) (shot_t *shot); - void (*fce_shot_transform_lasser) (shot_t *shot); -} export_fce_t; - -typedef struct module_s { - char *name; -#ifndef __WIN32__ - void *image; -#else /* __WIN32__ */ - HINSTANCE *image; -#endif /* __WIN32__ */ - - int (*fce_init) (export_fce_t *p); -#ifndef PUBLIC_SERVER - int (*fce_draw) (int x, int y, int w, int h); -#endif /* PUBLIC_SERVER */ - int (*fce_event) (); - int (*fce_isConflict) (int x, int y, int w, int h); - void (*fce_cmd) (char *line); - void (*fce_recvMsg) (char *msg); - int (*fce_destroy) (); - -} module_t; - -/* module's call list */ -typedef struct { - void *init; - void *draw; - void *event; - void *isConflict; - void *cmdArena; - void *recvMsg; - void *destroy; -} mod_sym_t; - -/* structure of the registered modules */ -typedef struct mod_context { - struct mod_context *next, *prev; - - char *name; - mod_sym_t *sym; -} mod_reg_t; - -/* available modules */ -extern mod_sym_t modai_sym; -extern mod_sym_t modwall_sym; -extern mod_sym_t modpipe_sym; -extern mod_sym_t modmove_sym; -extern mod_sym_t modbasic_sym; -extern mod_sym_t modteleport_sym; - -extern void module_init(); -extern int module_load(char *name); -extern int module_load_dep(char *name); -#ifndef PUBLIC_SERVER -extern void module_draw(int x, int y, int w, int h); -#endif /* PUBLIC_SERVER */ -extern void module_event(); -extern void module_cmd(char *s); -extern int module_is_conflict(int x, int y, int w, int h); -extern int module_recv_msg(char *msg); -extern void module_quit(); - -#endif /* MODULES_H */ diff --git a/src/base/msg.h b/src/base/msg.h deleted file mode 100644 index 403100c..0000000 --- a/src/base/msg.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MSG_H -#define MSG_H - -#ifdef DEBUG -#define debug(msg,arg...) fprintf(stdout, "%s ", _("[Debug]")); fprintf(stdout, _(msg), ##arg); fprintf(stdout, "\n") -#else /* DEBUG */ -#define debug(msg,arg...) -#endif /* DEBUG */ - -#define warning(msg,arg...) fprintf(stderr, "%s ", _("[Warning]")); fprintf(stderr, _(msg), ##arg); fprintf(stderr, "\n") -#define error(msg,arg...) fprintf(stderr, "%s ", _("[Error]")); fprintf(stderr, _(msg), ##arg); fprintf(stderr, "\n") -#define fatal(msg,arg...) \ -{ \ - fprintf(stderr, "%s ", _("[Fatal]")); fprintf(stderr, _(msg), ##arg); fprintf(stderr, "\n"); \ - abort(); \ -} - -#endif /* MSG_H */ diff --git a/src/base/myTimer.c b/src/base/myTimer.c deleted file mode 100644 index 5015cc6..0000000 --- a/src/base/myTimer.c +++ /dev/null @@ -1,157 +0,0 @@ -#include <time.h> -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/time.h> -#include <signal.h> -#ifdef __WIN32__ -#include <time.h> -#endif /* __WIN32__ */ - -#include "main.h" -#include "list.h" -#include "myTimer.h" -#ifndef PUBLIC_SERVER -#include "interface.h" -#endif /* PUBLIC_SERVER */ - -static struct timeval my_start = {.tv_sec = 0, .tv_usec = 0}; - -list_t *timer_new() -{ - return list_new(); -} - -void timer_restart() -{ - gettimeofday(&my_start, NULL); -} - - -my_time_t timer_get_current_time() -{ - struct timeval now; - my_time_t ticks; - - if (my_start.tv_sec == 0 && my_start.tv_usec == 0) { - timer_restart(); - } - - gettimeofday(&now, NULL); - ticks = (now.tv_sec - my_start.tv_sec) * 1000 + (now.tv_usec - my_start.tv_usec) / 1000; - /*printf("-> %d\n", ticks);*/ - - return ticks; -} - -my_time_t timer_get_current_timeMicro() -{ - static struct timeval my_micro_start = {.tv_sec = 0,.tv_usec = 0 }; - struct timeval now; - my_time_t ticks; - - if (my_micro_start.tv_sec == 0 && my_micro_start.tv_usec == 0) { - gettimeofday(&my_micro_start, NULL); - } - - gettimeofday(&now, NULL); - ticks = (now.tv_sec - my_micro_start.tv_sec) * 1000 * 1000 + (now.tv_usec - my_micro_start.tv_usec); - /*printf("-> %d\n", ticks);*/ - - return ticks; -} - -my_timer_t *timer_newItem(int type, void (*fce) (void *p), void *arg, my_time_t my_time) -{ - static int new_id = 0; - my_timer_t *new; - - assert(fce != NULL); - - new = malloc(sizeof(my_timer_t)); - memset(new, 0, sizeof(my_timer_t)); - new->id = new_id++; - new->type = type; - new->fce = fce; - new->arg = arg; - new->createTime = timer_get_current_time(); - new->time = my_time; - - return new; -} - -static void timer_destroyItem(my_timer_t *p) -{ - assert(p != NULL); - free(p); -} - -int timer_add_task(list_t *listTimer, int type, void (*fce) (void *p), void *arg, my_time_t my_time) -{ - my_timer_t *new; - - new = timer_newItem(type, fce, arg, my_time); - list_add(listTimer, new); - - return new->id; -} - -void timer_event(list_t *listTimer) -{ - int i; - my_timer_t *thisTimer; - my_time_t currentTime; - - currentTime = timer_get_current_time(); - - for (i = 0; i < listTimer->count; i++) { - thisTimer = (my_timer_t *) listTimer->list[i]; - assert(thisTimer != NULL); - - switch (thisTimer->type) { - case TIMER_ONE: - if (currentTime >= thisTimer->createTime + thisTimer->time) { - thisTimer->fce(thisTimer->arg); - list_del_item(listTimer, i, free); - i--; - } - break; - case TIMER_PERIODIC: - if (currentTime >= thisTimer->createTime + thisTimer->time) { - thisTimer->fce(thisTimer->arg); - thisTimer->createTime = timer_get_current_time(); - } - break; - default: - fatal("Bad setting of the timer"); - break; - } - } -} - -void timer_del(list_t *listTimer, int id) -{ - my_timer_t *thisTimer; - int i; - - for (i = 0; i < listTimer->count; i++) { - thisTimer = (my_timer_t *) listTimer->list[i]; - - assert(thisTimer != NULL); - - if (thisTimer->id == (unsigned) id) { - list_del_item(listTimer, i, free); - return; - } - } - - error("Unable to delete event from the timer as it is not present in it [%d]", id); - assert(0); -} - - -void timer_destroy(list_t *listTimer) -{ - list_destroy_item(listTimer, timer_destroyItem); -} diff --git a/src/base/myTimer.h b/src/base/myTimer.h deleted file mode 100644 index db05365..0000000 --- a/src/base/myTimer.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MY_TIME_H -#define MY_TIME_H - -#include "main.h" -#include "list.h" - -#define my_time_t unsigned int -#define TIMER_ONE 0 -#define TIMER_PERIODIC 1 - -typedef struct my_timer_struct { - unsigned int id; - void (*fce) (void *p); - void *arg; - int type; - my_time_t createTime; - my_time_t time; -} my_timer_t; - -extern list_t *timer_new(); -extern void timer_restart(); -extern my_time_t timer_get_current_time(); -extern int timer_add_task(list_t *listTimer, int type, void (*fce) (void *p), - void *arg, my_time_t my_time); -extern void timer_event(list_t *listTimer); -extern void timer_del(list_t *listTimer, int id); -extern void timer_destroy(list_t *listTimer); - -#endif /* MY_TIME_H */ diff --git a/src/base/net_multiplayer.c b/src/base/net_multiplayer.c deleted file mode 100644 index 9888f03..0000000 --- a/src/base/net_multiplayer.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <unistd.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "arenaFile.h" -#include "net_multiplayer.h" -#include "server.h" - -#include "udp.h" - -#ifndef PUBLIC_SERVER -#include "setting.h" -#include "choiceArena.h" - -#include "screen.h" -#include "client.h" -#endif /* PUBLIC_SERVER */ - -static int netGameType; - -int net_multiplayer_get_game_type() -{ - return netGameType; -} - -int net_multiplayer_init_for_game_server(char *ip4, char *ip6, int port) -{ - int ret; - - ret = server_init(ip4, ip6, port); - - if (ret > 0) { - netGameType = NET_GAME_TYPE_SERVER; - } - - return ret; -} - -int net_multiplayer_init(int type, char *ip, int port, int proto) -{ - netGameType = type; - - switch (netGameType) { - case NET_GAME_TYPE_NONE: - break; - - case NET_GAME_TYPE_SERVER: - switch (proto) { - case PROTO_UDPv4: - if (server_init(ip, NULL, port) != 1) { - error("Unable to initialize multiplayer game"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } - break; - - case PROTO_UDPv6: - if (server_init(NULL, ip, port) != 1) { - error("Unable to initialize multiplayer game"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } - break; - } - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT: - if (client_init(ip, port) != 0) { - error("Unable to join multiplayer game"); - netGameType = NET_GAME_TYPE_NONE; - return -1; - } - break; -#endif /* PUBLIC_SERVER */ - - default: - error("Unknown type of the network game [%d]", netGameType); - assert(0); - break; - } - - return 0; -} - -void net_multiplayer_event() -{ - switch (netGameType) { - case NET_GAME_TYPE_NONE: - break; - - case NET_GAME_TYPE_SERVER: - server_event(); - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT: - client_event(); - break; -#endif /* PUBLIC_SERVER */ - - default: - error("Unknown type of the network game [%d]", netGameType); - assert(0); - break; - } -} - -void net_multiplayer_quit() -{ - switch (netGameType) { - case NET_GAME_TYPE_NONE: - break; - - case NET_GAME_TYPE_SERVER: - server_quit(); - break; - -#ifndef PUBLIC_SERVER - case NET_GAME_TYPE_CLIENT: - client_quit(); - break; -#endif /* PUBLIC_SERVER */ - - default: - error("Unknown type of the network game [%d]", netGameType); - assert(0); - break; - } - - netGameType = NET_GAME_TYPE_NONE; -} diff --git a/src/base/net_multiplayer.h b/src/base/net_multiplayer.h deleted file mode 100644 index c52b1ee..0000000 --- a/src/base/net_multiplayer.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef NET_MULTIPLAYER_H -#define NET_MULTIPLAYER_H - -#include "main.h" -#include "tux.h" -#include "item.h" -#include "arena.h" - -#define PROTO_UDPv4 0 -#define PROTO_UDPv6 1 - -#define NET_GAME_TYPE_NONE 0 -#define NET_GAME_TYPE_SERVER 1 -#define NET_GAME_TYPE_CLIENT 2 - -#define NET_STATUS_OK 0 -#define NET_STATUS_ZOMBIE 1 - -#define NET_PROTOCOL_TYPE_TCP 0 -#define NET_PROTOCOL_TYPE_UDP 1 - -#define LIMIT_BUFFER 10240 - -#define NET_MASTER_SERVER_DOMAIN "ms.tuxanci.org" -#define NET_MASTER_SERVER "83.167.234.16" -#define NET_MASTER_PORT 6800 - -#ifdef PUBLIC_SERVER -extern int net_multiplayer_init_for_game_server(char *ip4, char *ip6, int port); -#endif /* PUBLIC_SERVER */ - -extern int net_multiplayer_init(int type, char *ip, int port, int proto); -extern int net_multiplayer_get_game_type(); -extern void net_multiplayer_event(); -extern void net_multiplayer_quit(); - -#endif /* NET_MULTIPLAYER_H */ diff --git a/src/base/path.h.in b/src/base/path.h.in deleted file mode 100644 index 8dfded9..0000000 --- a/src/base/path.h.in +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef PATH_H -#define PATH_H - -#include "main.h" - -#cmakedefine USE_MAGICK -#define APPNAME "@APPNAME@" -#ifdef __WIN32__ - #define PATH_DIR "data" PATH_SEPARATOR -#else - #define PATH_DIR "@CMAKE_DATA_PATH@" PATH_SEPARATOR APPNAME PATH_SEPARATOR -#endif -#define TUXANCI_VERSION "@TUXANCI_VERSION@" -#define PREFIX "@CMAKE_INSTALL_PREFIX@" PATH_SEPARATOR -#ifdef PUBLIC_SERVER - #define SERVER_CONFIG "@CMAKE_CONF_PATH@" PATH_SEPARATOR APPNAME PATH_SEPARATOR "server.conf" -#endif /* PUBLIC_SERVER */ -#define PATH_IMAGE PATH_DIR "images" PATH_SEPARATOR -#define PATH_ARENA PATH_DIR "arena" PATH_SEPARATOR -#define PATH_SOUND PATH_DIR "sound" PATH_SEPARATOR -#define PATH_MUSIC PATH_DIR "music" PATH_SEPARATOR -#define PATH_LOCALE "@CMAKE_LOCALE_PATH@" PATH_SEPARATOR -#define PATH_DOC "@CMAKE_DOC_PATH@" PATH_SEPARATOR -#define PACKAGE APPNAME - -#endif /* PATH_H */ diff --git a/src/base/protect.c b/src/base/protect.c deleted file mode 100644 index 754fa26..0000000 --- a/src/base/protect.c +++ /dev/null @@ -1,86 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "myTimer.h" -#include "protect.h" - -protect_t *newProtect() -{ - protect_t *new; - - new = malloc(sizeof(protect_t)); - new->lastMove = timer_get_current_time(); - new->lastPing = timer_get_current_time(); - new->avarage = 0; - new->count = 0; - new->isDown = FALSE; - - return new; -} - -void refreshLastMove(protect_t *p) -{ - my_time_t currentTime; - my_time_t interval; - - currentTime = timer_get_current_time(); - interval = currentTime - p->lastMove; - - p->lastMove = timer_get_current_time(); - p->avarage += interval; - p->count++; - - if (p->count == PROTECT_SPEED_AVARAGE) { - int my_index; - - my_index = p->avarage / PROTECT_SPEED_AVARAGE; - p->avarage = 0; - p->count = 0; - -/* - if (my_index < PROTECT_SPEED_INTERVAL_TIMEOUT) { - p->isDown = TRUE; - } -*/ - - /*printf("speed index = %d\n", my_index);*/ - } -} - -void rereshLastPing(protect_t *p) -{ - /* - my_time_t currentTime; - my_time_t interval; - currentTime = timer_get_current_time(); - interval = currentTime - p->lastPing; - */ - - p->lastPing = timer_get_current_time(); - - /*printf("interval = %d\n", interval);*/ -} - -bool_t isDown(protect_t *p) -{ - my_time_t currentTime; - my_time_t interval; - - currentTime = timer_get_current_time(); - interval = currentTime - p->lastPing; - - if (interval > PROTECT_PING_INTERVAL_TIMEOUT) { - p->isDown = TRUE; - } - - return p->isDown; -} - -void destroyProtect(protect_t *p) -{ - assert(p != NULL); - free(p); -} diff --git a/src/base/protect.h b/src/base/protect.h deleted file mode 100644 index 1a55ef7..0000000 --- a/src/base/protect.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef PROTECT_H -#define PROTECT_H - -#include "main.h" -#include "myTimer.h" - -#define PROTECT_PING_INTERVAL_TIMEOUT 5000 -#define PROTECT_SPEED_INTERVAL_TIMEOUT 40 -#define PROTECT_SPEED_AVARAGE 10 - -typedef struct protect_struct { - my_time_t lastPing; - my_time_t lastMove; - my_time_t avarage; - int count; - bool_t isDown; -} protect_t; - -extern protect_t *newProtect(); -extern void refreshLastMove(protect_t *p); -extern void rereshLastPing(protect_t *p); -extern bool_t isDown(protect_t *p); -extern void destroyProtect(protect_t *p); - -#endif /* PROTECT_H */ diff --git a/src/base/proto.c b/src/base/proto.c deleted file mode 100644 index 422a304..0000000 --- a/src/base/proto.c +++ /dev/null @@ -1,937 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "myTimer.h" -#include "server.h" -#include "proto.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "idManager.h" -#include "modules.h" -#include "protect.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "world.h" -#include "setting.h" -#include "choiceArena.h" -#include "analyze.h" -#include "downArena.h" -#include "client.h" -#include "term.h" -#include "radar.h" -#include "chat.h" -#else /* PUBLIC_SERVER*/ -#include "highScore.h" -#include "publicServer.h" -#include "serverConfigFile.h" -#endif /* PUBLIC_SERVER*/ - -void proto_send_error_server(int type, client_t *client, int errorcode) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "error %d\n", errorcode); - - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_error_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int errorcode; - int recv_count; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d", cmd, &errorcode); - - if (recv_count != 2) { - return; - } - - debug("Communication error [%d]", errorcode); - - switch (errorcode) { - case PROTO_ERROR_CODE_BAD_VERSION: - analyze_set_msg(_("Cannot connect to the server. You have incompatible version of the game.")); - world_do_end(); - break; - case PROTO_ERROR_CODE_TIMEOUT: - analyze_set_msg(_("Connection timeout")); - world_do_end(); - break; - case PROTO_ERROR_LIMIT_MAX_CLIENT: - analyze_set_msg(_("Server is full")); - world_do_end(); - break; - } -} - -void proto_send_hello_client(char *name) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "hello %s %s\n", - getParamElse("--version", TUXANCI_VERSION), name); - - debug("Sending: %s", msg); - client_send(msg); -} -#endif /* PUBLIC_SERVER */ - -static void action_sendItem(space_t *space, item_t *item, client_t *client) -{ - proto_send_additem_server(PROTO_SEND_ONE, client, item); -} - -static void sendInfoCreateClient(client_t *client) -{ - list_t *listClient; - client_t *thisClient; - tux_t *thisTux; - int i; - - assert(client != NULL); - - listClient = server_get_list_clients(); - - proto_send_init_server(PROTO_SEND_ONE, client, client); - -#ifndef PUBLIC_SERVER - proto_send_newtux_server(PROTO_SEND_ONE, client, world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT)); -#endif /* PUBLIC_SERVER */ - - for (i = 0; i < listClient->count; i++) { - thisClient = (client_t *) listClient->list[i]; - thisTux = thisClient->tux; - - /* odosli kazdemu stremu hracovy informacie o novom hracovy */ - /* send to all players information about new player */ - if (thisTux != NULL && thisTux != client->tux) { - proto_send_newtux_server(PROTO_SEND_ONE, thisClient, client->tux); - } - - /* send to new players information about all players, including himself */ - if (thisTux != NULL) { - proto_send_newtux_server(PROTO_SEND_ONE, client, thisTux); - } - } - - space_action(arena_get_current()->spaceItem, action_sendItem, client); - -/* - for (i = 0; i < arena_get_current()->spaceItem->listIndex->count; i++) { - thisItem = (item_t *) arena_get_current()->spaceItem->list->list[i]; - proto_send_additem_server(PROTO_SEND_ONE, client, thisItem); - } -*/ -} - -void proto_recv_hello_server(client_t *client, char *msg) -{ - static char *supportVersion = NULL; - char cmd[STR_PROTO_SIZE]; - char version[STR_NAME_SIZE]; - char name[STR_NAME_SIZE]; - int recv_count; - - assert(client != NULL); - - if (space_get_count(arena_get_current()->spaceTux) + 1 > server_get_max_clients()) { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_LIMIT_MAX_CLIENT); - check_front_event(client); - client->status = NET_STATUS_ZOMBIE; - return; - } - - strcpy(version, ""); - strcpy(name, ""); - - if (supportVersion == NULL) { -#ifndef PUBLIC_SERVER - supportVersion = TUXANCI_VERSION; -#else /* PUBLIC_SERVER */ - supportVersion = public_server_get_setting("SUPPORT_CLIENTS", "--support-clients", TUXANCI_VERSION); -#endif /* PUBLIC_SERVER */ - } - - recv_count = sscanf(msg, "%s %s %s", cmd, version, name); - - if (recv_count != 3) { - return; - } - - if (strstr(supportVersion, version) == NULL) { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_VERSION); - check_front_event(client); - client->status = NET_STATUS_ZOMBIE; - return; - } - - client->tux = tux_new(); - client->tux->control = TUX_CONTROL_NET; - client->tux->client = client; - - space_add(arena_get_current()->spaceTux, client->tux); - - if (strlen(name) > STR_NAME_SIZE - 1) { - name[STR_NAME_SIZE - 1] = '\0'; - } - - tux_set_name(client->tux, name); - sendInfoCreateClient(client); -} - -void proto_send_status_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - char *name; - char *version; - int clients; - int maxclients; - unsigned int uptime; - char *arena; - -#ifndef PUBLIC_SERVER - name = "NewServer"; -#endif /* PUBLIC_SERVER */ - -#ifdef PUBLIC_SERVER - name = public_server_get_setting("NAME", "--name", "noname"); -#endif /* PUBLIC_SERVER */ - - version = TUXANCI_VERSION; - clients = arena_get_current()->spaceTux->listIndex->count; - maxclients = server_get_max_clients(); - uptime = (unsigned int) server_get_update(); - arena = arena_file_get_net_name(choice_arena_get()); - - snprintf(msg, STR_PROTO_SIZE, - "name: %s\n" - "version: %s\n" - "clients: %d\n" - "maxclients: %d\n" - "uptime: %d\n" - "arena: %s\n", - name, version, clients, maxclients, uptime, arena); - - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); - /*proto_send(type, client, msg);*/ -} - -void proto_recv_status_server(client_t *client, char *msg) -{ - proto_send_status_server(PROTO_SEND_ONE, client); -} - -#ifndef PUBLIC_SERVER -void proto_send_check_client(int id) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "check %d\n", id); - client_send(msg); -} -#else /* PUBLIC_SERVER */ -void proto_send_listscore_server(int type, client_t *client, int max) -{ - char *str; - char *msg; - int i; - - msg = malloc((max * 48) * sizeof(char)); - strcpy(msg, ""); - - for (i = 0; i < max; i++) { - str = high_score_get_table(i); - - if (str == NULL) { - break; - } - - strcat(msg, str); - strcat(msg, "\n"); - } - - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); - free(msg); -} - -void proto_recv_listscore_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int recv_count; - int max; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d", cmd, &max); - - if (recv_count != 2) { - max = HIGHSCORE_MAX_PLAYERS; - } - - proto_send_listscore_server(PROTO_SEND_ONE, client, max); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_check_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int recv_count; - int id; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d", cmd, &id); - - if (recv_count != 2) { - return; - } - - check_front_del_msg(client->listSendMsg, id); -} - -void proto_send_init_server(int type, client_t *client, client_t *client2) -{ - char msg[STR_PROTO_SIZE]; - int count; - int check_id; - - assert(client2 != NULL); - - count = WORLD_COUNT_ROUND_UNLIMITED; - -#ifndef PUBLIC_SERVER - public_server_get_settingCountRound(&count); -#endif /* PUBLIC_SERVER */ - - check_id = id_get_newcount(0); - - snprintf(msg, STR_PROTO_SIZE, "init %d %d %d %d %s %d\n", - client2->tux->id, client2->tux->x, client2->tux->y, - count, arena_file_get_net_name(choice_arena_get()), check_id); - - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_CHECK, check_id); -} - -#ifndef PUBLIC_SERVER -void proto_recv_init_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char arena_name[STR_PROTO_SIZE]; - tux_t *tux; - int id; - int x, y, n; - int check_id; - int recv_count; - arena_t *arena; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d %d %d %d %s %d", - cmd, &id, &x, &y, &n, arena_name, &check_id); - - if (recv_count != 7) { - return; - } - - proto_send_check_client(check_id); - - if (arena_get_current() != NULL) { - return; - } - - world_set_arena(arena_file_get_file_format_net_name(arena_name)); - - arena = arena_get_current(); - arena->max_countRound = n; - - tux = tux_new(); - tux_replace_id(tux, id); - tux->x = x; - tux->y = y; - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_RIGHT); - - public_server_get_setting_name_right(tux->name); - - space_add(arena->spaceTux, tux); -} -#endif /* PUBLIC_SERVER */ - -void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "event %d %d\n", tux->id, action); - - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_event_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, action; - tux_t *tux; - int recv_count; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d %d", cmd, &id, &action); - - if (recv_count != 3) { - return; - } - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux != NULL) { - tux_action(tux, action); - radar_add(tux->id, tux->x, tux->y, RADAR_TYPE_TUX); - } -} - -void proto_send_event_client(int action) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "event %d\n", action); - - client_send(msg); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_event_server(client_t *client, char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int action; - int recv_count; - - assert(client != NULL); - assert(msg != NULL); - - /*debug_interval();*/ - - recv_count = sscanf(msg, "%s %d", cmd, &action); - - if (recv_count != 2) { - return; - } - - if (client->tux->bonus != BONUS_HIDDEN) { - proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, client, client->tux, action); - } - - refreshLastMove(client->protect); - - tux_action(client->tux, action); -} - -void proto_send_newtux_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - int x, y; - - assert(tux != NULL); - - if (tux->bonus == BONUS_HIDDEN) { - x = TUX_LOCATE_UNKNOWN; - y = TUX_LOCATE_UNKNOWN; - } else { - tux_get_proportion(tux, &x, &y, NULL, NULL); - } - - snprintf(msg, STR_PROTO_SIZE, "newtux %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n", - tux->id, x, y, tux->status, tux->position, tux->frame, - tux->score, tux->name, tux->gun, tux->bonus, - tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], - tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], - tux->shot[GUN_LASSER], tux->shot[GUN_MINE], - tux->shot[GUN_BOMBBALL], tux->bonus_time, tux->pickup_time); - - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, - CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_newtux_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - char name[STR_NAME_SIZE]; - int id, x, y, status, position, frame, score; - int myGun, myBonus; - int gun1, gun2, gun3, gun4, gun5, gun6, gun7; - int time1, time2; - tux_t *tux; - int recv_count; - - assert(msg != NULL); - - if (arena_get_current() == NULL) { - return; - } - - recv_count = sscanf(msg, "%s %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", - cmd, &id, &x, &y, &status, &position, &frame, &score, name, - &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, - &gun7, &time1, &time2); - - if (recv_count != 20) { - return; - } - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux != NULL) { - assert(tux->id == id); - } - - if (tux == NULL) { - tux = tux_new(); - tux_replace_id(tux, id); - tux->control = TUX_CONTROL_NET; - space_add(arena_get_current()->spaceTux, tux); - } - - if (tux->control == TUX_CONTROL_KEYBOARD_RIGHT && - x == TUX_LOCATE_UNKNOWN && y == TUX_LOCATE_UNKNOWN) { - tux_get_proportion(tux, &x, &y, NULL, NULL); - } - - radar_add(id, x, y, RADAR_TYPE_TUX); - - if (tux->bonus == BONUS_HIDDEN) { - radar_del(id); - } - - space_move_object(arena_get_current()->spaceTux, tux, x, y); - tux->status = status; - tux->position = position; - tux->frame = frame; - tux->score = score; - strcpy(tux->name, name); - tux->gun = myGun; - tux->bonus = myBonus; - tux->shot[GUN_SIMPLE] = gun1; - tux->shot[GUN_DUAL_SIMPLE] = gun2; - tux->shot[GUN_SCATTER] = gun3; - tux->shot[GUN_TOMMY] = gun4; - tux->shot[GUN_LASSER] = gun5; - tux->shot[GUN_MINE] = gun6; - tux->shot[GUN_BOMBBALL] = gun7; - tux->bonus_time = time1; - tux->pickup_time = time2; -} -#endif /* PUBLIC_SERVER */ - -void proto_send_kill_server(int type, client_t *client, tux_t *tux) -{ - char msg[STR_PROTO_SIZE]; - - assert(tux != NULL); - - snprintf(msg, STR_PROTO_SIZE, "kill %d\n", tux->id); - - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_kill_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id; - tux_t *tux; - int recv_count; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d", cmd, &id); - - if (recv_count != 2) { - return; - } - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux != NULL) { - tux_event_tux_is_dead(tux); - } -} -#endif /* PUBLIC_SERVER */ - -void proto_send_del_server(int type, client_t *client, int id) -{ - char msg[STR_PROTO_SIZE]; - int check_id; - - check_id = id_get_newcount(0); - - snprintf(msg, STR_PROTO_SIZE, "del %d %d\n", id, check_id); - - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_CHECK, check_id); - /*proto_check(type, client, msg, check_id);*/ -} - -#ifndef PUBLIC_SERVER - -void timer_delShot(void *p) -{ - shot_t *shot; - int *id; - - assert( p != NULL ); - id = (int *) p; - - shot = space_get_object_id(arena_get_current()->spaceShot, *id); - - if (shot != NULL) { - radar_del(shot->id); - space_del_with_item(arena_get_current()->spaceShot, shot, shot_destroy); - } - - free(p); -} - -void proto_recv_del_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, check_id; - tux_t *tux; - shot_t *shot; - item_t *item; - int recv_count; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d %d", cmd, &id, &check_id); - - if (recv_count != 3) { - return; - } - - proto_send_check_client(check_id); - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux != NULL) { - radar_del(id); - space_del_with_item(arena_get_current()->spaceTux, tux, tux_destroy); - return; - } - - shot = space_get_object_id(arena_get_current()->spaceShot, id); - - if (shot != NULL) { - /* - printf("delete shot..\n"); - radar_del(id); - space_del_with_item(arena_get_current()->spaceShot, shot, shot_destroy); - */ - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, timer_delShot, newInt(id), 50); - return; - } - - item = space_get_object_id(arena_get_current()->spaceItem, id); - - if (item != NULL) { - /*printf("delete item..\n");*/ - radar_del(id); - space_del_with_item(arena_get_current()->spaceItem, item, item_destroy); - return; - } -} -#endif /* PUBLIC_SERVER */ - -void proto_send_additem_server(int type, client_t *client, item_t *p) -{ - char msg[STR_PROTO_SIZE]; - int check_id; - - assert(p != NULL); - - check_id = id_get_newcount(0); - - snprintf(msg, STR_PROTO_SIZE, "additem %d %d %d %d %d %d %d %d\n", - p->id, p->type, p->x, p->y, p->count, p->frame, p->author_id, check_id); - - /* - proto_send(type, client, msg); - proto_check(type, client, msg, check_id); - */ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_CHECK, check_id); -} - -#ifndef PUBLIC_SERVER -void proto_recv_additem_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, type, x, y, count, frame, author_id, check_id; - item_t *item; - int recv_count; - - assert(msg != NULL); - - if (arena_get_current() == NULL) { - return; - } - - recv_count = sscanf(msg, "%s %d %d %d %d %d %d %d %d", - cmd, &id, &type, &x, &y, &count, &frame, &author_id, &check_id); - - if (recv_count != 9) { - return; - } - - proto_send_check_client(check_id); - - if ((item = space_get_object_id(arena_get_current()->spaceItem, id)) != NULL) { - return; - } - - if (type != ITEM_MINE && type != ITEM_EXPLOSION && - type != ITEM_BIG_EXPLOSION) { - radar_add(id, x, y, RADAR_TYPE_ITEM); - } - - item = item_new(x, y, type, author_id); - - item_replace_id(item, id); - item->count = count; - item->frame = frame; - - space_add(arena_get_current()->spaceItem, item); -} -#endif /* PUBLIC_SERVER */ - -void proto_send_shot_server(int type, client_t *client, shot_t *p) -{ - char msg[STR_PROTO_SIZE]; - - assert(p != NULL); - - snprintf(msg, STR_PROTO_SIZE, "shot %d %d %d %d %d %d %d %d %d\n", - p->id, p->x, p->y, p->px, p->py, p->position, p->gun, - p->author_id, p->isCanKillAuthor); - - /* - proto_send(type, client, msg); - proto_check(type, client, msg, check_id); - */ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_shot_client(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor; - shot_t *shot; - int recv_count; - - assert(msg != NULL); - - recv_count = sscanf(msg, "%s %d %d %d %d %d %d %d %d %d", - cmd, &shot_id, &x, &y, &px, &py, &position, &gun, - &author_id, &isCanKillAuthor); - - if (recv_count != 10) { - return; - } - - if ((shot = space_get_object_id(arena_get_current()->spaceShot, shot_id)) != NULL) { - space_del_with_item(arena_get_current()->spaceShot, shot, shot_destroy); - /*return;*/ - } - - shot = shot_new(x, y, px, py, gun, author_id); - - shot_replace_id(shot, shot_id); - shot->isCanKillAuthor = isCanKillAuthor; - shot->position = position; - - if (shot->gun == GUN_LASSER) { - shot_transform_lasser(shot); - } - - space_add(arena_get_current()->spaceShot, shot); -} - -void proto_send_chat_client(char *s) -{ - char msg[STR_PROTO_SIZE]; - snprintf(msg, STR_PROTO_SIZE, "chat %s\n", s); - client_send(msg); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_chat_server(client_t *client, char *msg) -{ - char out[STR_PROTO_SIZE]; - int len; - - if (client->tux == NULL) { - return; - } - - len = strlen(msg); - msg[len - 1] = '\0'; - - snprintf(out, STR_PROTO_SIZE, "chat %s:%s\n", client->tux->name, msg + 5); - - proto_send_chat_server(PROTO_SEND_ALL, NULL, out); -} - -void proto_send_chat_server(int type, client_t *client, char *msg) -{ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); - -#ifndef PUBLIC_SERVER - proto_recv_chat_client(msg); -#endif /* PUBLIC_SERVER */ -} - -#ifndef PUBLIC_SERVER -void proto_recv_chat_client(char *msg) -{ - int len; - - assert(msg != NULL); - - len = strlen(msg); - msg[len - 1] = '\0'; - - chat_add(msg + 5); -} - -void proto_send_module_client(char *msg) -{ - char out[STR_PROTO_SIZE]; - - snprintf(out, STR_PROTO_SIZE, "module %s\n", msg); - -/* - strcpy(out, "modules "); - strcat(out, msg); - strcat(out, "\n"); -*/ - - client_send(out); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_module_server(client_t *client, char *msg) -{ - module_recv_msg(msg + 7); -} - -void proto_send_module_server(int type, client_t *client, char *msg) -{ - char out[STR_PROTO_SIZE]; - - snprintf(out, STR_PROTO_SIZE, "module %s\n", msg); - -/* - strcpy(out, "modules "); - strcat(out, msg); - strcat(out, "\n"); -*/ - - send_msg_to_client(type, client, out, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_module_client(char *msg) -{ - module_recv_msg(msg + 7); -} - -void proto_send_ping_client() -{ - char msg[STR_PROTO_SIZE]; - snprintf(msg, STR_PROTO_SIZE, "ping\n"); - client_send(msg); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_ping_server(client_t *client, char *msg) -{ -} - -void proto_send_ping_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - snprintf(msg, STR_PROTO_SIZE, "ping\n"); - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_ping_client(char *msg) -{ -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_echo_server(client_t *client, char *msg) -{ - proto_send_echo_server(PROTO_SEND_ONE, client, msg + 4); /* msg + strlen("echo"); */ -} - -void proto_send_echo_server(int type, client_t *client, char *s) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "echo%s", s); - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -void proto_send_end_server(int type, client_t *client) -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "end\n"); - /*proto_send(type, client, msg);*/ - send_msg_to_client(type, client, msg, CHECK_FRONT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); -} - -#ifndef PUBLIC_SERVER -void proto_recv_end_client(char *msg) -{ - assert(msg != NULL); - - world_do_end(); -} - -void proto_send_end_client() -{ - char msg[STR_PROTO_SIZE]; - - snprintf(msg, STR_PROTO_SIZE, "end\n"); - client_send(msg); -} -#endif /* PUBLIC_SERVER */ - -void proto_recv_end_server(client_t *client, char *msg) -{ - assert(msg != NULL); - assert(client != NULL); - - client->status = NET_STATUS_ZOMBIE; -} diff --git a/src/base/proto.h b/src/base/proto.h deleted file mode 100644 index a743b8c..0000000 --- a/src/base/proto.h +++ /dev/null @@ -1,98 +0,0 @@ -#ifndef MY_PROTO_H -#define MY_PROTO_H - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "myTimer.h" -#include "server.h" -#include "net_multiplayer.h" - -#ifndef PUBLIC_SERVER -#include "world.h" -#include "setting.h" -#include "choiceArena.h" -#include "client.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -#define PROTO_ERROR_CODE_UNKNOWN 0 -#define PROTO_ERROR_CODE_BAD_VERSION 1 -#define PROTO_ERROR_CODE_BAD_NAME 2 -#define PROTO_ERROR_CODE_BAD_COMMAND 3 -#define PROTO_ERROR_CODE_TIMEOUT 4 -#define PROTO_ERROR_LIMIT_MAX_CLIENT 5 - -extern void proto_send_error_server(int type, client_t *client, int errorcode); -extern void proto_recv_error_client(char *msg); - -extern void proto_send_hello_client(char *name); -extern void proto_recv_hello_server(client_t *client, char *msg); - -extern void proto_send_status_server(int type, client_t *client); -extern void proto_recv_status_server(client_t *client, char *msg); - -extern void proto_send_listscore_server(int type, client_t *client, int max); -extern void proto_recv_listscore_server(client_t *client, char *msg); - -extern void proto_send_check_client(int id); -extern void proto_recv_check_server(client_t *client, char *msg); - -extern void proto_send_init_server(int type, client_t *client, client_t *client2); -extern void proto_recv_init_client(char *msg); - -extern void proto_send_event_server(int type, client_t *client, tux_t *tux, int action); -extern void proto_recv_event_client(char *msg); -extern void proto_send_event_client(int action); -extern void proto_recv_event_server(client_t *client, char *msg); - -extern void proto_send_newtux_server(int type, client_t *client, tux_t *tux); -extern void proto_recv_newtux_client(char *msg); - -extern void proto_send_kill_server(int type, client_t *client, tux_t *tux); -extern void proto_recv_kill_client(char *msg); - -extern void proto_send_score_server(int type, client_t *client, tux_t *tux); -extern void proto_recv_score_client(char *msg); - -extern void proto_send_del_server(int type, client_t *client, int id); -extern void proto_recv_del_client(char *msg); - -extern void proto_send_additem_server(int type, client_t *client, item_t *p); -extern void proto_recv_additem_client(char *msg); - -extern void proto_send_shot_server(int type, client_t *client, shot_t *p); -extern void proto_recv_shot_client(char *msg); - -extern void proto_send_chat_client(char *s); -extern void proto_recv_chat_server(client_t *client, char *msg); -extern void proto_send_chat_server(int type, client_t *client, char *msg); -extern void proto_recv_chat_client(char *msg); - -extern void proto_send_module_client(char *s); -extern void proto_recv_module_server(client_t *client, char *msg); -extern void proto_send_module_server(int type, client_t *client, char *msg); -extern void proto_recv_module_client(char *msg); - -extern void proto_send_ping_client(); -extern void proto_recv_ping_server(client_t *client, char *msg); -extern void proto_send_ping_server(int type, client_t *client); -extern void proto_recv_ping_client(char *msg); - -extern void proto_recv_echo_server(client_t *client, char *msg); -extern void proto_send_echo_server(int type, client_t *client, char *s); - -extern void proto_send_end_server(int type, client_t *client); -extern void proto_recv_end_client(char *msg); -extern void proto_send_end_client(); -extern void proto_recv_end_server(client_t *client, char *msg); - -#endif /* MY_PROTO_H */ diff --git a/src/base/server.c b/src/base/server.c deleted file mode 100644 index 6f26859..0000000 --- a/src/base/server.c +++ /dev/null @@ -1,383 +0,0 @@ -#ifndef __WIN32__ -#include <sys/socket.h> -#include <sys/types.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#else /* __WIN32__ */ -#include <windows.h> -#include <wininet.h> -#endif /* __WIN32__ */ - -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <assert.h> -#include <fcntl.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "proto.h" -#include "server.h" -#include "myTimer.h" -#include "arena.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "protect.h" -#include "index.h" -#include "serverSelect.h" -#include "serverSendMsg.h" -#include "downServer.h" - -#ifndef PUBLIC_SERVER -#include "world.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#include "highScore.h" -#endif /* PUBLIC_SERVER */ - -#include "udp_server.h" - -static list_t *listClient; - -static list_t *listServerTimer; -static time_t timeUpdate; -static int maxClients; - -typedef struct proto_cmd_server_struct { - char *name; - int len; - int tux; - void (*fce_proto) (client_t *, char *msg); -} proto_cmd_server_t; - -static proto_cmd_server_t proto_cmd_list[] = { - {.name = "hello",.len = 5,.tux = 0,.fce_proto = proto_recv_hello_server}, - {.name = "status",.len = 6,.tux = 0,.fce_proto = proto_recv_status_server}, -#ifdef PUBLIC_SERVER - {.name = "list",.len = 4,.tux = 0,.fce_proto = proto_recv_listscore_server}, -#endif /* PUBLIC_SERVER */ - {.name = "event",.len = 5,.tux = 1,.fce_proto = proto_recv_event_server}, - {.name = "check",.len = 5,.tux = 1,.fce_proto = proto_recv_check_server}, - {.name = "module",.len = 6,.tux = 1,.fce_proto = proto_recv_module_server}, - {.name = "chat",.len = 4,.tux = 1,.fce_proto = proto_recv_chat_server}, - {.name = "ping",.len = 4,.tux = 1,.fce_proto = proto_recv_ping_server}, - {.name = "echo",.len = 4,.tux = 0,.fce_proto = proto_recv_echo_server}, - {.name = "end",.len = 3,.tux = 1,.fce_proto = proto_recv_end_server}, - {.name = "",.len = 0,.tux = 0,.fce_proto = NULL}, -}; - -static proto_cmd_server_t *findCmdProto(client_t *client, char *msg) -{ - int len; - int i; - - len = strlen(msg); - - for (i = 0; proto_cmd_list[i].len != 0; i++) { - proto_cmd_server_t *thisCmd; - - thisCmd = &proto_cmd_list[i]; - - if (len >= thisCmd->len && strncmp(msg, thisCmd->name, thisCmd->len) == 0) { - if ((thisCmd->tux == 1 && client->tux == NULL) || - (thisCmd->tux == 0 && client->tux != NULL)) { - return NULL; - } - - return thisCmd; - } - } - - return NULL; -} - -static void startUpdateServer() -{ - timeUpdate = time(NULL); -} - -time_t server_get_update() -{ - return time(NULL) - timeUpdate; -} - -client_t *server_new_any_client() -{ - client_t *new; - - new = malloc(sizeof(client_t)); - memset(new, 0, sizeof(client_t)); - - new->status = NET_STATUS_OK; - new->listRecvMsg = list_new(); - new->listSendMsg = check_front_new(); - new->listSeesShot = list_new(); - new->protect = newProtect(); - - return new; -} - -void server_destroy_any_client(client_t *p) -{ - assert(p != NULL); - - /*check_front_event(p);*/ - list_destroy_item(p->listRecvMsg, free); - list_destroy_item(p->listSeesShot, free); - check_front_destroy(p->listSendMsg); - - destroyProtect(p->protect); - - if (p->tux != NULL) { -#ifdef PUBLIC_SERVER - table_add(p->tux->name, p->tux->score); -#endif /* PUBLIC_SERVER */ - space_del_with_item(arena_get_current()->spaceTux, p->tux, tux_destroy); - } - - free(p); -} - -static void eventDelClientFromListClient(client_t *client) -{ - int offset; - - offset = list_search(listClient, client); - - assert(offset != -1); - - list_del_item(listClient, offset, server_udp_destroy_client); -} - -static void delZombieCLient(void *p_nothing) -{ - client_t *thisClient; - my_time_t currentTime; - int i; - - currentTime = timer_get_current_time(); - - for (i = 0; i < listClient->count; i++) { - thisClient = (client_t *) listClient->list[i]; - - if (isDown(thisClient->protect) == TRUE) { - proto_send_end_server(PROTO_SEND_ONE, thisClient); - check_front_event(thisClient); - thisClient->status = NET_STATUS_ZOMBIE; - } - - if (thisClient->status == NET_STATUS_ZOMBIE) { - if (thisClient->tux != NULL) { - proto_send_del_server(PROTO_SEND_ALL, NULL, thisClient->tux->id); - } - - eventDelClientFromListClient(thisClient); - i--; - } - } -} - -static void eventPeriodicSyncClient(void *p_nothing) -{ - client_t *thisClientSend; - tux_t *thisTux; - int i; - -#ifndef PUBLIC_SERVER - proto_send_newtux_server(PROTO_SEND_ALL_SEES_TUX, NULL, world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT)); -#endif /* PUBLIC_SERVER */ - - for (i = 0; i < listClient->count; i++) { - thisClientSend = (client_t *) listClient->list[i]; - thisTux = thisClientSend->tux; - - if (thisTux == NULL) { - continue; - } - - proto_send_newtux_server(PROTO_SEND_ALL_SEES_TUX, thisClientSend, thisTux); - /*proto_send_newtux_server(PROTO_SEND_BUT, thisClientSend, thisTux);*/ - } -} - -static void eventSendPingClients(void *p_nothing) -{ - proto_send_ping_server(PROTO_SEND_ALL, NULL); -} - -void server_set_time() -{ - if (listServerTimer != NULL) { - timer_destroy(listServerTimer); - } - - timer_restart(); - listServerTimer = timer_new(); - - timer_add_task(listServerTimer, TIMER_PERIODIC, delZombieCLient, NULL, SERVER_TIMEOUT); - timer_add_task(listServerTimer, TIMER_PERIODIC, eventPeriodicSyncClient, NULL, SERVER_TIME_SYNC); - timer_add_task(listServerTimer, TIMER_PERIODIC, eventSendPingClients, NULL, SERVER_TIME_PING); -} - -int server_init(char *ip4, char *ip6, int port) -{ - int ret; - - startUpdateServer(); - listServerTimer = NULL; - - listClient = list_new(); - - server_set_max_clients(SERVER_MAX_CLIENTS); - server_set_time(); - - ret = server_udp_init(ip4, ip6, port); - - if (ret == 0) { - return -1; - } - - /* initialize the arena download server */ - if (ip4 != NULL) { - down_server_init(ip4, port); - } else if (ip6 != NULL) { - down_server_init(ip6, port); - } - - return ret; -} - -list_t *server_get_list_clients() -{ - return listClient; -} - -int server_get_max_clients() -{ - return maxClients; -} - -void server_set_max_clients(int n) -{ - maxClients = n; -} - -void server_send_client(client_t *p, char *msg) -{ - assert(p != NULL); - assert(msg != NULL); - - if (p->status != NET_STATUS_ZOMBIE) { - int ret = -1; - -#ifndef PUBLIC_SERVER - if (isParamFlag("--send")) { - debug("Sending: %s", msg); - } -#endif /* PUBLIC_SERVER */ - - ret = sock_udp_write(p->socket_udp, p->socket_udp, msg, strlen(msg)); - - if (ret <= 0) { - p->status = NET_STATUS_ZOMBIE; - } - } -} - -static void client_eventWorkRecvList(client_t *client) -{ - proto_cmd_server_t *protoCmd; - char *line; - int i; - - assert(client != NULL); - - /* processing of events from clients */ - for (i = 0; i < client->listRecvMsg->count; i++) { - line = (char *) client->listRecvMsg->list[i]; - protoCmd = findCmdProto(client, line); - -#ifndef PUBLIC_SERVER - if (isParamFlag("--recv")) { - debug("Received: %s", line); - } -#endif /* PUBLIC_SERVER */ - - if (protoCmd != NULL) { - protoCmd->fce_proto(client, line); - rereshLastPing(client->protect); - } else { - proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND); - } - } - - list_destroy_item(client->listRecvMsg, free); - client->listRecvMsg = list_new(); -} - -static void porcesListClients() -{ - int i; - client_t *thisClient; - - for (i = 0; i < listClient->count; i++) { - thisClient = (client_t *) listClient->list[i]; - - client_eventWorkRecvList(thisClient); - check_front_event(thisClient); - } -} - -void server_event() -{ -#ifndef PUBLIC_SERVER - int count; - - do { - select_restart(); - - server_udp_set_select(); - down_server_set_select(); - - select_action(); - - count = server_udp_select_sock(); - down_server_select_socket(); - } while (count > 0); -#else /* PUBLIC_SERVER */ - int ret; - - select_restart(); - - server_udp_set_select(); - down_server_set_select(); - - ret = select_action(); - - if (ret > 0) { - server_udp_select_sock(); - down_server_select_socket(); - } -#endif /* PUBLIC_SERVER */ - - porcesListClients(); - timer_event(listServerTimer); -} - -void server_quit() -{ - proto_send_end_server(PROTO_SEND_ALL, NULL); - - assert(listClient != NULL); - - list_destroy_item(listClient, server_udp_destroy_client); - - timer_destroy(listServerTimer); - - server_udp_quit(); - - down_server_quit(); -} diff --git a/src/base/server.h b/src/base/server.h deleted file mode 100644 index 72d7585..0000000 --- a/src/base/server.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef MY_SERVER_H -#define MY_SERVER_H - -#include <time.h> - -#include "list.h" -#include "tux.h" -#include "myTimer.h" -#include "protect.h" -#include "buffer.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#endif /* PUBLIC_SERVER */ - -#include "udp.h" - -#define SERVER_TIMEOUT 1000 -#define SERVER_TIME_SYNC 1000 -#define SERVER_TIME_PING 1000 -#define SERVER_MAX_CLIENTS 100 - -#define SERVER_INDEX_ROOT_TUX 0 - -#define CLIENT_TYPE_UDP 1 -#define CLIENT_TYPE_TCP 2 - -typedef struct client_struct { - int type; - - sock_udp_t *socket_udp; - - buffer_t *recvBuffer; - buffer_t *sendBuffer; - - int status; - - tux_t *tux; - - protect_t *protect; - - list_t *listSendMsg; - list_t *listRecvMsg; - - list_t *listSeesShot; -} client_t; - -extern int server_init(char *ip4, char *ip6, int port); -extern time_t server_get_update(); -extern client_t *server_new_any_client(); -extern void server_destroy_any_client(client_t *p); -extern list_t *server_get_list_clients(); -extern int server_get_max_clients(); -extern void server_set_time(); -extern void server_set_max_clients(int n); -extern void server_send_client(client_t *p, char *msg); -extern void server_event(); -extern void server_quit(); - -#endif /* MY_SERVER_H */ diff --git a/src/base/serverSelect.c b/src/base/serverSelect.c deleted file mode 100644 index 6f8d64e..0000000 --- a/src/base/serverSelect.c +++ /dev/null @@ -1,105 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <sys/types.h> - -#include <unistd.h> -#include <fcntl.h> -#include <sys/time.h> - -#ifndef __WIN32__ -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/select.h> -#else /* __WIN32__ */ -#include <io.h> -#include <winsock2.h> -#endif /* __WIN32__ */ - -#include "server.h" - -static struct timeval tv; -static fd_set readfds; -static fd_set writefds; -static int max_fd; - -void select_restart() -{ -#ifndef PUBLIC_SERVER - tv.tv_sec = 0; - tv.tv_usec = 0; -#else /* PUBLIC_SERVER */ - tv.tv_sec = 0; - tv.tv_usec = 1000; -#endif /* PUBLIC_SERVER */ - - FD_ZERO(&readfds); - FD_ZERO(&writefds); - max_fd = 0; -} - -void select_add_sock_for_read(int sock) -{ - /*printf("select_add_sock_for_read %d\n", sock);*/ - - FD_SET(sock, &readfds); - - if (sock > max_fd) { - max_fd = sock; - } -} - -void select_add_sock_for_write(int sock) -{ - /*printf("select_add_sock_for_write %d\n", sock);*/ - - FD_SET(sock, &writefds); - - if (sock > max_fd) { - max_fd = sock; - } -} - -int select_action() -{ - int ret; - -#ifndef PUBLIC_SERVER - ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, &tv); -#else /* PUBLIC_SERVER */ - list_t *listClient; - listClient = server_get_list_clients(); - - if (listClient->count == 0) { - ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, NULL); - server_set_time(); - } else { - ret = select(max_fd + 1, &readfds, &writefds, (fd_set *) NULL, &tv); - } -#endif /* PUBLIC_SERVER */ - - return ret; -} - -int select_is_change_sock_for_read(int sock) -{ - int ret; - - ret = FD_ISSET(sock, &readfds); - - /*printf("select_is_change_sock %d -> %d\n", sock, ret);*/ - - return ret; -} - -int select_is_change_sock_for_write(int sock) -{ - int ret; - - ret = FD_ISSET(sock, &writefds); - - /*printf("select_is_change_sock %d -> %d\n", sock, ret);*/ - - return ret; -} diff --git a/src/base/serverSelect.h b/src/base/serverSelect.h deleted file mode 100644 index e1880cb..0000000 --- a/src/base/serverSelect.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef SERVER_SELECT_H -#define SERVER_SELECT_H - -extern void select_restart(); -extern void select_add_sock_for_read(int sock); -extern void select_add_sock_for_write(int sock); -extern int select_action(); -extern int select_is_change_sock(int sock); -extern int select_is_change_sock_for_read(int sock); -extern int select_is_change_sock_for_write(int sock); - -#endif /* SERVER_SELECT_H */ diff --git a/src/base/serverSendMsg.c b/src/base/serverSendMsg.c deleted file mode 100644 index d74103e..0000000 --- a/src/base/serverSendMsg.c +++ /dev/null @@ -1,145 +0,0 @@ -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <assert.h> - -#include "server.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "world.h" -#endif /* PUBLIC_SERVER */ - -static void addMsgClient(client_t *p, char *msg, int type, int id) -{ - assert(p != NULL); - assert(msg != NULL); - - if (p->status != NET_STATUS_ZOMBIE) { - check_front_msg_add(p->listSendMsg, msg, type, id); - } -} - -static void addMsgAllClientBut(char *msg, client_t *p, int type, int id) -{ - list_t *listClient; - client_t *thisClient; - int i; - - assert(msg != NULL); - - listClient = server_get_list_clients(); - - for (i = 0; i < listClient->count; i++) { - thisClient = (client_t *) listClient->list[i]; - - if (thisClient->tux != NULL && thisClient != p) { - addMsgClient(thisClient, msg, type, id); - } - } -} - -static void addMsgAllClient(char *msg, int type, int id) -{ - assert(msg != NULL); - - addMsgAllClientBut(msg, NULL, type, id); -} - -static void addMsgAllClientSeesTux(char *msg, tux_t *tux, int type, int id) -{ - list_t *listHelp; - arena_t *arena; - space_t *space; - int x, y, w, h; - int i; - - assert(msg != NULL); - - arena = arena_get_current(); - space = arena->spaceTux; - - x = tux->x - WINDOW_SIZE_X; - y = tux->y - WINDOW_SIZE_Y; - - w = 2 * WINDOW_SIZE_X; - h = 2 * WINDOW_SIZE_Y; - - if (x < 0) { - x = 0; - } - - if (y < 0) { - y = 0; - } - - if (w + x >= arena->w) { - w = arena->w - (x + 1); - } - - if (h + y >= arena->h) { - h = arena->h - (y + 1); - } - - listHelp = list_new(); - - space_get_object(space, x, y, w, h, listHelp); - /*printf("%d %d %d %d %d\n", x, y, w, h, listHelp->count);*/ - - for (i = 0; i < listHelp->count; i++) { - tux_t *thisTux; - client_t *thisClient; - - thisTux = (tux_t *) listHelp->list[i]; - - if (thisTux == tux) { - continue; - } - - thisClient = thisTux->client; - - if (thisClient != NULL) { - addMsgClient(thisClient, msg, type, id); - } - } - - list_destroy(listHelp); -} - -void send_msg_to_client(int type, client_t *client, char *msg, int type2, int id) -{ - assert(msg != NULL); - - switch (type) { - case PROTO_SEND_ONE: - assert(client != NULL); - addMsgClient(client, msg, type2, id); - break; - case PROTO_SEND_ALL: - assert(client == NULL); - addMsgAllClient(msg, type2, id); - break; - case PROTO_SEND_BUT: - assert(client != NULL); - addMsgAllClientBut(msg, client, type2, id); - break; - case PROTO_SEND_ALL_SEES_TUX: -#ifndef PUBLIC_SERVER - if (client != NULL) { - addMsgAllClientSeesTux(msg, client->tux, type2, id); - } else { - addMsgAllClientSeesTux(msg, world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT), type2, id); - } -#else /* PUBLIC_SERVER*/ - addMsgAllClientSeesTux(msg, client->tux, type2, id); -#endif /* PUBLIC_SERVER */ - break; - default: - error("Unknown type of target group of players [%d]", type); - assert(0); - break; - } -} diff --git a/src/base/serverSendMsg.h b/src/base/serverSendMsg.h deleted file mode 100644 index 68bd65c..0000000 --- a/src/base/serverSendMsg.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SERVER_SEND_MSG_H -#define SERVER_SEND_MSG_H - -#include "server.h" - -#define PROTO_SEND_ONE 0 -#define PROTO_SEND_ALL 1 -#define PROTO_SEND_BUT 3 -#define PROTO_SEND_ALL_SEES_TUX 4 - -extern void send_msg_to_client(int type, client_t *client, char *msg, int type2, int id); - -#endif /* SERVER_SEND_MSG_H */ diff --git a/src/base/shareFunction.c b/src/base/shareFunction.c deleted file mode 100644 index a9cd69d..0000000 --- a/src/base/shareFunction.c +++ /dev/null @@ -1,65 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "shareFunction.h" - -typedef struct share_fce_item_struct { - char *name; - void *function; -} share_fce_item_t; - -static list_t *listShareFce; - -static share_fce_item_t *newShareFceItem(char *name, void *function) -{ - share_fce_item_t *new; - - new = malloc(sizeof(share_fce_item_t)); - new->name = strdup(name); - new->function = function; - return new; -} - -static void destroyShareFce(share_fce_item_t *p) -{ - free(p->name); - free(p); -} - -void share_function_init() -{ - listShareFce = list_new(); -} - -void share_function_add(char *name, void *function) -{ - debug("Adding new shared function [%s]", name); - - list_add(listShareFce, newShareFceItem(name, function)); -} - -void *share_function_get(char *name) -{ - int i; - - for (i = 0; i < listShareFce->count; i++) { - share_fce_item_t *this; - - this = (share_fce_item_t *) listShareFce->list[i]; - - if (strcmp(this->name, name) == 0) { - return this->function; - } - } - - return NULL; -} - -void share_function_quit() -{ - list_destroy_item(listShareFce, destroyShareFce); -} diff --git a/src/base/shareFunction.h b/src/base/shareFunction.h deleted file mode 100644 index a444842..0000000 --- a/src/base/shareFunction.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SHARE_FUNCTION_H -#define SHARE_FUNCTION_H - -extern void share_function_init(); -extern void share_function_add(char *name, void *function); -extern void *share_function_get(char *name); -extern void share_function_quit(); - -#endif /* SHARE_FUNCTION_H */ diff --git a/src/base/shot.c b/src/base/shot.c deleted file mode 100644 index 0d30750..0000000 --- a/src/base/shot.c +++ /dev/null @@ -1,347 +0,0 @@ -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "shot.h" -#include "gun.h" -#include "net_multiplayer.h" -#include "proto.h" -#include "idManager.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "image.h" -#include "layer.h" -#include "world.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -#ifndef PUBLIC_SERVER -static image_t *g_shot_simple; -static image_t *g_shot_lasserX; -static image_t *g_shot_lasserY; -static image_t *g_shot_bombball; -#endif /* PUBLIC_SERVER */ - -static bool_t isShotInit = FALSE; - -bool_t shot_is_inicialized() -{ - return isShotInit; -} - -void shot_init() -{ -#ifndef PUBLIC_SERVER - assert(image_is_inicialized() == TRUE); - - g_shot_simple = image_add("shot.png", IMAGE_ALPHA, "shot", IMAGE_GROUP_BASE); - g_shot_lasserX = image_add("lasserX.png", IMAGE_NO_ALPHA, "lasserX", IMAGE_GROUP_BASE); - g_shot_lasserY = image_add("lasserY.png", IMAGE_NO_ALPHA, "lasserY",IMAGE_GROUP_BASE); - g_shot_bombball = image_add("bombball.png", IMAGE_ALPHA, "bombball", IMAGE_GROUP_BASE); -#endif /* PUBLIC_SERVER */ - - isShotInit = TRUE; -} - -shot_t *shot_new(int x, int y, int px, int py, int gun, int author_id) -{ - shot_t *new; - tux_t *author; - - new = malloc(sizeof(shot_t)); - memset(new, 0, sizeof(shot_t)); - - new->id = id_get_new(); - new->x = x; - new->y = y; - new->px = px; - new->py = py; - new->gun = gun; - new->author_id = author_id; - - new->position = POSITION_UNKNOWN; - - author = space_get_object_id(arena_get_current()->spaceTux, author_id); - - if (author != NULL) { - new->position = author->position; - } - - new->isCanKillAuthor = FALSE; - new->del = FALSE; - - switch (gun) { - case GUN_SIMPLE: - new->w = GUN_SHOT_WIDTH; - new->h = GUN_SHOT_HEIGHT; -#ifndef PUBLIC_SERVER - new->img = g_shot_simple; -#endif /* PUBLIC_SERVER */ - break; - - case GUN_LASSER: - switch (author->position) { - case TUX_RIGHT: - case TUX_LEFT: - new->w = GUN_LASSER_HORIZONTAL; - new->h = GUN_SHOT_VERTICAL; -#ifndef PUBLIC_SERVER - new->img = g_shot_lasserX; -#endif /* PUBLIC_SERVER */ - break; - case TUX_UP: - case TUX_DOWN: - new->w = GUN_SHOT_VERTICAL; - new->h = GUN_LASSER_HORIZONTAL; -#ifndef PUBLIC_SERVER - new->img = g_shot_lasserY; -#endif /* PUBLIC_SERVER */ - break; - } - break; - - case GUN_BOMBBALL: - new->w = GUN_BOMBBALL_WIDTH; - new->h = GUN_BOMBBALL_HEIGHT; - -#ifndef PUBLIC_SERVER - new->img = g_shot_bombball; -#endif /* PUBLIC_SERVER */ - break; - - default: - error("Unknown type of gun [%d]", gun); - assert(0); - break; - } - - return new; -} - -void shot_replace_id(shot_t *shot, int id) -{ - id_replace(shot->id, id); - shot->id = id; -} - - -void shot_get_status(void *p, int *id, int *x, int *y, int *w, int *h) -{ - shot_t *shot; - - shot = p; - *id = shot->id; - *x = shot->x; - *y = shot->y; - *w = shot->w; - *h = shot->h; -} - -void shot_set_status(void *p, int x, int y, int w, int h) -{ - shot_t *shot; - - shot = p; - shot->x = x; - shot->y = y; - shot->w = w; - shot->h = h; -} - -#ifndef PUBLIC_SERVER -void shot_draw(shot_t *p) -{ - assert(p != NULL); - addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, TUX_LAYER); -} - -void shot_draw_list(list_t *listShot) -{ - shot_t *thisShot; - int i; - - assert(listShot != NULL); - - for (i = 0; i < listShot->count; i++) { - thisShot = (shot_t *) listShot->list[i]; - assert(thisShot != NULL); - shot_draw(thisShot); - } -} -#endif /* PUBLIC_SERVER */ - -static int getRandomCourse(int x, int y) -{ - int ret = -1; - - do { - switch (random() % 3) { - case 0: - ret = y; - break; - case 1: - ret = -y; - break; - case 2: - ret = 0; - break; - } - } while (ret == x); - - return ret; -} - -void shot_bound_bombBall(shot_t *shot) -{ - if (shot->gun != GUN_BOMBBALL) { - return; - } - - shot->px = getRandomCourse(shot->px, +10); - shot->py = getRandomCourse(shot->py, +10); - shot->isCanKillAuthor = 1; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); - } -} - -void shot_transform_lasser(shot_t *shot) -{ - space_t *space; - int mustRefesh = 0; - - space = arena_get_current()->spaceShot; - - if (space_get_object_id(space, shot->id) != NULL) { - mustRefesh = 1; - } - - if (mustRefesh) { - space_del(space, shot); - } - - switch (shot->position) { - case TUX_RIGHT: - case TUX_LEFT: - shot->w = GUN_LASSER_HORIZONTAL; - shot->h = GUN_SHOT_VERTICAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserX; -#endif /* PUBLIC_SERVER */ - break; - - case TUX_UP: - case TUX_DOWN: - shot->w = GUN_SHOT_VERTICAL; - shot->h = GUN_LASSER_HORIZONTAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserY; -#endif /* PUBLIC_SERVER */ - break; - } - - if (mustRefesh) { - space_add(space, shot); - } -} - -static void action_moveShot(space_t *space, shot_t *shot, void *p) -{ - int new_x, new_y; - arena_t *arena; - - arena = arena_get_current(); - - new_x = shot->x + shot->px; - new_y = shot->y + shot->py; - - space_move_object(space, shot, new_x, new_y); - - if (shot->x + shot->w < 0 || shot->x > arena->w || - shot->y + shot->h < 0 || shot->y > arena->h) { - space_del_with_item(space, shot, shot_destroy); - } -} - -void shot_event_move_list(arena_t *arena) -{ - space_action(arena->spaceShot, action_moveShot, NULL); -} - -static int isValueInList(list_t *list, int x) -{ - int i; - - for (i = 0; i < list->count; i++) { - if (x == (*(int *) list->list[i])) { - return 1; - } - } - - return 0; -} - -static void action_check(space_t *space, shot_t *shot, client_t *client) -{ - if (isValueInList(client->listSeesShot, shot->id) == 0) { - list_add(client->listSeesShot, newInt(shot->id)); - proto_send_shot_server(PROTO_SEND_ONE, client, shot); - } -} - -void shot_check_is_tux_screen(arena_t *arena) -{ - int screen_x, screen_y; - tux_t *thisTux; - client_t *thisClient; - list_t *listClient; - int i; - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_SERVER) { - return; - } - - listClient = server_get_list_clients(); - - for (i = 0; i < listClient->count; i++) { - thisClient = (client_t *) listClient->list[i]; - thisTux = (tux_t *) thisClient->tux; - int del = 0; - - if (thisTux == NULL || thisTux->control != TUX_CONTROL_NET) { - continue; - } - - arena_get_center_screen(&screen_x, &screen_y, thisTux->x, thisTux->y, - WINDOW_SIZE_X, WINDOW_SIZE_Y); - - space_action_from_location(arena->spaceShot, action_check, - thisClient, screen_x, screen_y, - WINDOW_SIZE_X, WINDOW_SIZE_Y); - - while (thisClient->listSeesShot->count > 100) { - list_del_item(thisClient->listSeesShot, 0, free); - del++; - } - - /*if( del > 0 )printf("del = %d\n", del);*/ - } -} - -void shot_destroy(shot_t *p) -{ - assert(p != NULL); - - id_del(p->id); - - free(p); -} - -void shot_quit() -{ - isShotInit = FALSE; -} diff --git a/src/base/shot.h b/src/base/shot.h deleted file mode 100644 index 97b7a91..0000000 --- a/src/base/shot.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef SHOT_H -#define SHOT_H - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "arena.h" -#ifndef PUBLIC_SERVER -#include "image.h" -#endif /* PUBLIC_SERVER */ - -#ifdef __WIN32__ -#define random rand -#endif /* __WIN32__ */ - -typedef struct shot_struct { - int id; - int x; - int y; - int w; - int h; - int px; - int py; - int position; - int gun; - int author_id; - bool_t isCanKillAuthor; - bool_t del; -#ifndef PUBLIC_SERVER - image_t *img; -#endif /* PUBLIC_SERVER */ -} shot_t; - -extern bool_t shot_is_inicialized(); -extern void shot_init(); -extern shot_t *shot_new(int x, int y, int px, int py, int gun, int author_id); -extern shot_t *shot_get_id(list_t *listShot, int id); -extern void shot_replace_id(shot_t *shot, int id); -extern void shot_get_status(void *p, int *id, int *x, int *y, int *w, int *h); -extern void shot_set_status(void *p, int x, int y, int w, int h); -#ifndef PUBLIC_SERVER -extern void shot_draw(shot_t *p); -extern void shot_draw_list(list_t *listShot); -#endif /* PUBLIC_SERVER */ -extern int shot_is_conflict_with_list_shot(list_t *listShot, int x, int y, int w, int h); -extern void shot_event_move_list(arena_t *arena); -extern void shot_check_is_tux_screen(arena_t *arena); -extern void shot_bound_bombBall(shot_t *shot); -extern void shot_transform_lasser(shot_t *shot); -extern void shot_destroy(shot_t *p); -extern void shot_quit(); - -#endif /* SHOT_H */ diff --git a/src/base/space.c b/src/base/space.c deleted file mode 100644 index 6ef2892..0000000 --- a/src/base/space.c +++ /dev/null @@ -1,375 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "space.h" -#include "index.h" - -zone_t *newZone() -{ - zone_t *new; - - new = malloc(sizeof(zone_t)); - new->list = list_new(); - - return new; -} - -void destroyZone(zone_t *p) -{ - assert(p != NULL); - - list_destroy(p->list); - free(p); -} - -static int my_arena_conflict_space(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) -{ - return (x1 < x2 + w2 && x2 < x1 + w1 && y1 < y2 + h2 && y2 < y1 + h1); -} - -space_t *space_new(int w, int h, int segW, int segH, - void (*getStatus) (void *p, int *id, int *x, int *y, int *w, int *h), - void (*setStatus) (void *p, int x, int y, int w, int h)) -{ - space_t *new; - int i, j; - - assert(w > 0 && h > 0 && segW > 0 && segH > 0); - - new = malloc(sizeof(space_t)); - - if (!new) { - return 0; - } - - memset(new, 0, sizeof(space_t)); - - new->w = w / segW + 1; - new->h = h / segH + 1; - new->segW = segW; - new->segH = segH; - new->getStatus = getStatus; - new->setStatus = setStatus; - new->listIndex = index_new(); - - new->zone = malloc(new->w * sizeof(list_t **)); - - for (i = 0; i < new->w; i++) { - new->zone[i] = malloc(new->h * sizeof(list_t *)); - } - - for (i = 0; i < new->h; i++) { - for (j = 0; j < new->w; j++) { - new->zone[j][i] = newZone(); - } - } - - return new; -} - -static void getSegment(space_t *p, int x, int y, int w, int h, - int *segX, int *segY, int *segW, int *segH) -{ - *segX = x / p->segW; - *segY = y / p->segH; - *segW = ((x + w) / p->segW + 1) - *segX; - *segH = ((y + h) / p->segH + 1) - *segY; -} - -int space_get_count(space_t *p) -{ - return p->listIndex->count; -} - -void *space_get_item(space_t *p, int offset) -{ - index_item_t *this; - - this = (index_item_t *) p->listIndex->list[offset]; - return this->data; -} - -void space_add(space_t *p, void *item) -{ - int segX, segY, segW, segH; - int id, x, y, w, h; - int i, j; - - if (!p || !item) { - return; - } - - if (!p->zone) { - return; - } - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for (i = segY; i < segY + segH; i++) { - for (j = segX; j < segX + segW; j++) { - if (j < 0 || j >= p->w || i < 0 || i >= p->h) { - continue; - } - - list_add(p->zone[j][i]->list, item); - } - } - - index_add(p->listIndex, id, item); -} - -void space_get_object(space_t *p, int x, int y, int w, int h, list_t *list) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for (i = segY; i < segY + segH; i++) { - for (j = segX; j < segX + segW; j++) { - void *this; - - if (j < 0 || j >= p->w || i < 0 || i >= p->h) { - continue; - } - - for (k = 0; k < p->zone[j][i]->list->count; k++) { - this = p->zone[j][i]->list->list[k]; - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if (my_arena_conflict_space(x, y, w, h, this_x, this_y, this_w, this_h) && - list_search(list, this) == -1) { - list_add(list, this); - } - } - } - } -} - -void *space_get_object_id(space_t *space, int id) -{ - return index_get(space->listIndex, id); -} - -int space_is_conflict_with_object(space_t *p, int x, int y, int w, int h) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for (i = segY; i < segY + segH; i++) { - for (j = segX; j < segX + segW; j++) { - void *this; - - if (j < 0 || j >= p->w || i < 0 || i >= p->h) { - continue; - } - - for (k = 0; k < p->zone[j][i]->list->count; k++) { - this = p->zone[j][i]->list->list[k]; - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if (my_arena_conflict_space(x, y, w, h, this_x, this_y, this_w, this_h)) { - return 1; - } - } - } - } - - return 0; -} - -int space_is_conflict_with_object_but(space_t *p, int x, int y, int w, int h, void *but) -{ - int segX, segY, segW, segH; - int id, this_x, this_y, this_w, this_h; - int i, j, k; - - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for (i = segY; i < segY + segH; i++) { - for (j = segX; j < segX + segW; j++) { - void *this; - - if (j < 0 || j >= p->w || i < 0 || i >= p->h) { - continue; - } - - for (k = 0; k < p->zone[j][i]->list->count; k++) { - this = p->zone[j][i]->list->list[k]; - - if (this == but) { - continue; - } - - p->getStatus(this, &id, &this_x, &this_y, &this_w, &this_h); - - if (my_arena_conflict_space(x, y, w, h, this_x, this_y, this_w, this_h)) { - return 1; - } - } - } - } - - return 0; -} - -void space_del(space_t *p, void *item) -{ - int segX, segY, segW, segH; - int id, x, y, w, h; - int myIndex; - int i, j; - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); - - for (i = segY; i < segY + segH; i++) { - for (j = segX; j < segX + segW; j++) { - if (j < 0 || j >= p->w || i < 0 || i >= p->h) { - continue; - } - - myIndex = list_search(p->zone[j][i]->list, item); - assert(myIndex != -1); - list_del(p->zone[j][i]->list, myIndex); - } - } - - index_del(p->listIndex, id); -} - -void space_del_with_item(space_t *p, void *item, void *f) -{ - void (*fce) (void *param); - - fce = f; - - space_del(p, item); - fce(item); -} - -void space_move_object(space_t *p, void *item, int move_x, int move_y) -{ - int old_segX, old_segY, old_segW, old_segH; - int new_segX, new_segY, new_segW, new_segH; - int id, x, y, w, h; - - p->getStatus(item, &id, &x, &y, &w, &h); - getSegment(p, x, y, w, h, &old_segX, &old_segY, &old_segW, &old_segH); - getSegment(p, move_x, move_y, w, h, &new_segX, &new_segY, &new_segW, &new_segH); - -/* - printf("%d %d %d %d -> %d %d %d %d \n", - old_segX, old_segY, old_segW, old_segH, - new_segX, new_segY, new_segW, new_segH); -*/ - - if (old_segX != new_segX || old_segY != new_segY || - old_segW != new_segW || old_segH != new_segH) { - space_del(p, item); - p->setStatus(item, move_x, move_y, w, h); - space_add(p, item); - return; - } - - p->setStatus(item, move_x, move_y, w, h); -} - -void space_print(space_t *p) -{ - int i, j; - - debug("Debugging space:"); - - for (i = 0; i < p->h; i++) { - for (j = 0; j < p->w; j++) { - printf("%3d ", p->zone[j][i]->list->count); - } - - putchar('\n'); - } -} - -void space_action(space_t *space, void *f, void *p) -{ - void (*fce) (space_t *space, void *f, void *p); - int len; - int i; - - fce = f; - len = space->listIndex->count; - - for (i = 0; i < len; i++) { - fce(space, space_get_item(space, i), p); - - if (space->listIndex->count == len - 1) { - len--; - i--; - } - } -} - -void space_action_from_location(space_t *space, void *f, void *p, int x, int y, int w, int h) -{ - void (*fce) (space_t *space, void *f, void *p); - list_t *list; - int len; - int i; - - fce = f; - - list = list_new(); - - space_get_object(space, x, y, w, h, list); - - len = list->count; - - for (i = 0; i < len; i++) { - fce(space, list->list[i], p); - } - - list_destroy(list); -} - -void space_destroy(space_t *p) -{ - int j, i; - - index_destroy(p->listIndex); - - for (i = 0; i < p->h; i++) { - for (j = 0; j < p->w; j++) { - destroyZone(p->zone[j][i]); - } - } - - for (i = 0; i < p->w; i++) { - free(p->zone[i]); - } - - free(p->zone); - free(p); -} - -void space_destroy_with_item(space_t *p, void *f) -{ - void (*fce) (void *param); - int i; - - fce = f; - - for (i = 0; i < p->listIndex->count; i++) { - fce(space_get_item(p, i)); - } - - space_destroy(p); -} diff --git a/src/base/space.h b/src/base/space.h deleted file mode 100644 index 8b888a7..0000000 --- a/src/base/space.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef SPACE_H -#define SPACE_H - -#include "main.h" -#include "list.h" - -typedef struct zone_struct { - list_t *list; -} zone_t; - -typedef struct space_struct { - int w; - int h; - - int segW; - int segH; - - zone_t ***zone; - - list_t *listIndex; - - void (*getStatus) (void *p, int *id, int *x, int *y, int *w, int *h); - void (*setStatus) (void *p, int x, int y, int w, int h); -} space_t; - -extern space_t *space_new(int w, int h, int segW, int segH, - void (*getStatus) (void *p, int *id, int *x, int *y, int *w, int *h), - void (*setStatus) (void *p, int x, int y, int w, int h)); - -extern int space_get_count(space_t *p); -extern void *space_get_item(space_t *p, int offset); -extern void space_add(space_t *p, void *item); -extern void space_get_object(space_t *p, int x, int y, int w, int h, list_t *list); -extern void *space_get_object_id(space_t *p, int id); -extern int space_is_conflict_with_object(space_t *p, int x, int y, int w, int h); -extern int space_is_conflict_with_object_but(space_t *p, int x, int y, int w, int h, void *but); -extern void space_del(space_t *p, void *item); -extern void space_del_with_item(space_t *p, void *item, void *f); -extern void space_move_object(space_t *p, void *item, int move_x, int move_y); -extern void space_print(space_t *p); -extern void space_action(space_t *space, void *f, void *p); -extern void space_action_from_location(space_t *space, void *f, void *p, - int x, int y, int w, int h); -extern void space_destroy(space_t *p); -extern void space_destroy_with_item(space_t *p, void *f); - -#endif /* SPACE_H */ diff --git a/src/base/storage.c b/src/base/storage.c deleted file mode 100644 index 842c560..0000000 --- a/src/base/storage.c +++ /dev/null @@ -1,136 +0,0 @@ -#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 *storage_new() -{ - return list_new(); -} - -static storage_item_t *storage_new_item(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 storage_destroy_item(storage_item_t *p, void *f) -{ - void (*fce) (void *); - - assert(p != NULL); - assert(f != NULL); - - fce = f; - fce(p->data); - free(p->name); - free(p->group); - free(p); -} - -void storage_add(list_t *list, char *group, char *name, void *data) -{ - assert(list != NULL); - assert(group != NULL); - assert(name != NULL); - - list_add(list, storage_new_item(group, name, data)); -} - -void *storage_get(list_t *list, char *group, char *name) -{ - storage_item_t *this; - int i; - - assert(list != NULL); - assert(group != NULL); - assert(name != NULL); - - 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; - } - } - - debug("Storage item not found [%s %s]", group, name); - - return NULL; -} - -void storage_del(list_t *list, char *group, char *name, void *f) -{ - storage_item_t *this; - int i; - - assert(list != NULL); - assert(group != NULL); - assert(name != NULL); - - 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) { - storage_destroy_item(this, f); - list_del(list, i); - return; - } - } - - return; -} - -void storage_del_all(list_t *list, char *group, void *f) -{ - storage_item_t *this; - int i; - - assert(list != NULL); - assert(group != NULL); - - for (i = 0; i < list->count; i++) { - this = (storage_item_t *) list->list[i]; - - if (strcmp(group, this->group) == 0) { - storage_destroy_item(this, f); - list_del(list, i); - i--; - } - } -} - -void storage_destroy(list_t *p, void *f) -{ - 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]; - - storage_destroy_item(this, f); - } - - list_destroy(p); -} diff --git a/src/base/storage.h b/src/base/storage.h deleted file mode 100644 index 1ef9b78..0000000 --- a/src/base/storage.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef STORAGE_H -#define STORAGE_H - -#include "main.h" -#include "list.h" - -extern list_t *storage_new(); -extern void storage_add(list_t *list, char *group, char *name, void *data); -extern void *storage_get(list_t *list, char *group, char *name); -extern void storage_del(list_t *list, char *group, char *name, void *f); -extern void storage_del_all(list_t *list, char *group, void *f); -extern void storage_destroy(list_t *p, void *f); - -#endif /* STORAGE_H */ diff --git a/src/base/string_length.h b/src/base/string_length.h deleted file mode 100644 index 43a6883..0000000 --- a/src/base/string_length.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef STR_LENGTH_H -#define STR_LENGTH_H - -#include "main.h" - -#define STR_SIZE 128 -#define STR_PROTO_SIZE 128 -#define STR_LOG_SIZE 256 -#define STR_NUM_SIZE 8 -#define STR_NAME_SIZE 32 -#define STR_PATH_SIZE 4096 -#define STR_FILE_NAME_SIZE 256 -#define STR_IP_SIZE 64 - -#endif /* STR_LENGTH_H */ diff --git a/src/base/textFile.c b/src/base/textFile.c deleted file mode 100644 index b1c2ad9..0000000 --- a/src/base/textFile.c +++ /dev/null @@ -1,151 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <assert.h> -#include <unistd.h> - -#include "main.h" -#include "textFile.h" - -textFile_t *text_file_new(char *s) -{ - textFile_t *new; - char path[STR_PATH_SIZE]; - - assert(s != NULL); - - if (isFillPath(s)) { - strcpy(path, s); - } else { - getcwd(path, STR_PATH_SIZE); - strcat(path, PATH_SEPARATOR); - strcat(path, s); - } - - new = malloc(sizeof(textFile_t)); - new->file = strdup(path); - new->text = list_new(); - - return new; -} - -static void createLine(list_t *list, char *p, int len) -{ - char *line; - char *begin_line; - char *end_line; - int length_line = 0; - - assert(list != NULL); - assert(p != NULL); - assert(len >= 0); - - begin_line = p; - - do { - end_line = memchr(begin_line, '\n', len); - - if (end_line == NULL) { - break; - } - - length_line = (int) (end_line - begin_line); - - line = malloc((length_line + 1) * sizeof(char)); - memset(line, 0, (length_line + 1) * sizeof(char)); - strncpy(line, begin_line, length_line); - - list_add(list, line); - len -= (length_line + 1); - - begin_line = end_line + 1; - - } while (begin_line != NULL); -} - -/** - * Loads file *s and returns him formatted to textFile_t* - */ -textFile_t *text_file_load(char *s) -{ - FILE *file; - textFile_t *ret; - char *p; - int file_length = 0; - struct stat buf; - - assert(s != NULL); - - if (lstat(s, &buf) < 0) { - error("Unable to get file status [%s]", s); - return NULL; - } - - file_length = buf.st_size; - if ((file = fopen(s, "rb")) == NULL) { - error("Unable to open file for reading [%s]", s); - return NULL; - } - - p = malloc(file_length * sizeof(char)); - - if (fread(p, file_length * sizeof(char), 1, file) != 1) { - error("Unable to read data from file [%s]", s); - fclose(file); - return NULL; - } - - fclose(file); - - ret = text_file_new(s); - createLine(ret->text, p, file_length); - - free(p); - - return ret; -} - -/** - * Prints the text saved in *p into stdout - */ -void text_file_print(textFile_t *p) -{ - int i; - - assert(p != NULL); - - debug("Printing file [%s]", p->file); - - for (i = 0; i < p->text->count; i++) { - printf("%3d >> %s\n", i, (char *) p->text->list[i]); - } -} - -void text_file_save(textFile_t *p) -{ - int i; - FILE *file; - - assert(p != NULL); - - file = fopen(p->file, "wb"); - - for (i = 0; i < p->text->count; i++) { - fprintf(file, "%s\n", (char *) p->text->list[i]); - } - - fclose(file); -} - -/** - * Removes text saved in *p from the memory - */ -void text_file_destroy(textFile_t *p) -{ - assert(p != NULL); - - free(p->file); - list_destroy_item(p->text, free); - free(p); -} diff --git a/src/base/textFile.h b/src/base/textFile.h deleted file mode 100644 index d2153fc..0000000 --- a/src/base/textFile.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef TEXTFILE_H -#define TEXTFILE_H - -#include "list.h" - -#ifdef __WIN32__ -#define lstat stat -#endif /* __WIN32__ */ - -typedef struct str_textFile_t { - char *file; - list_t *text; -} textFile_t; - -extern textFile_t *text_file_new(char *s); -extern textFile_t *text_file_load(char *s); -extern void text_file_print(textFile_t *p); -extern void text_file_save(textFile_t *p); -extern void text_file_destroy(textFile_t *p); - -#endif /* TEXTFILE_H */ diff --git a/src/base/tux.c b/src/base/tux.c deleted file mode 100644 index f88d6a4..0000000 --- a/src/base/tux.c +++ /dev/null @@ -1,766 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "tux.h" -#include "arena.h" -#include "shot.h" -#include "gun.h" -#include "item.h" -#include "arena.h" -#include "myTimer.h" -#include "net_multiplayer.h" -#include "proto.h" -#include "modules.h" -#include "idManager.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "font.h" -#include "image.h" -#include "layer.h" -#include "radar.h" - -#ifndef NO_SOUND -#include "sound.h" -#endif /* NO_SOUND */ - -#include "world.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -#ifndef PUBLIC_SERVER -static image_t *g_tux_up; -static image_t *g_tux_right; -static image_t *g_tux_left; -static image_t *g_tux_down; -static image_t *g_cross; -#endif /* PUBLIC_SERVER */ - -static bool_t isTuxInit = FALSE; - -bool_t tux_is_inicialized() -{ - return isTuxInit; -} - -void tux_init() -{ -#ifndef PUBLIC_SERVER - assert(image_is_inicialized() == TRUE); - - g_tux_up = image_add("tux8.png", IMAGE_ALPHA, "tux8", IMAGE_GROUP_BASE); - g_tux_right = image_add("tux6.png", IMAGE_ALPHA, "tux6", IMAGE_GROUP_BASE); - g_tux_left = image_add("tux4.png", IMAGE_ALPHA, "tux4", IMAGE_GROUP_BASE); - g_tux_down = image_add("tux2.png", IMAGE_ALPHA, "tux2", IMAGE_GROUP_BASE); - g_cross = image_add("cross.png", IMAGE_ALPHA, "cross", IMAGE_GROUP_BASE); -#endif /* PUBLIC_SERVER */ - isTuxInit = TRUE; -} - -int getRandomGun() -{ - return random() % (GUN_BOMBBALL+1); -} - -tux_t *tux_new() -{ - int x, y; - tux_t *new; - - new = malloc(sizeof(tux_t)); - assert(new != NULL); - - memset(new, 0, sizeof(tux_t)); - - new->id = id_get_new(); - new->status = TUX_STATUS_ALIVE; - new->control = TUX_CONTROL_NONE; - - arena_find_free_space(arena_get_current(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - tux_set_proportion(new, x, y); - - new->position = TUX_DOWN; - new->gun = getRandomGun(); - new->shot[new->gun] = GUN_MAX_SHOT; - - sprintf(new->name, "no_name_id_%d", new->id); - new->score = 0; - new->frame = 0; - - new->bonus = BONUS_NONE; - new->bonus_time = 0; - - new->isCanShot = TRUE; - new->isCanSwitchGun = TRUE; - - new->client = NULL; - - return new; -} - -void tux_set_name(tux_t *tux, char *name) -{ - strcpy(tux->name, name); - /*tux->g_name = getFontImage(name, COLOR_WHITE);*/ -} - -bool_t tux_is_any_gun(tux_t *tux) -{ - int i; - - for (i = 0; i < GUN_COUNT; i++) { - if (tux->shot[i] > 0) { - return TRUE; - } - } - - return FALSE; -} - -void tux_get_course(int n, int *x, int *y) -{ - assert(x != NULL); - assert(y != NULL); - - switch (n) { - case TUX_UP: - *x = 0; - *y = -1; - break; - - case TUX_LEFT: - *x = -1; - *y = 0; - break; - - case TUX_RIGHT: - *x = +1; - *y = 0; - break; - - case TUX_DOWN: - *x = 0; - *y = +1; - break; - - default: - error("Unknown direction of the tux [%d]", n); - assert(0); - break; - } -} - -#ifndef PUBLIC_SERVER -void tux_draw(tux_t *tux) -{ - image_t *g_image = NULL; - - assert(tux != NULL); - - if (tux->bonus == BONUS_HIDDEN && - net_multiplayer_get_game_type() != NET_GAME_TYPE_NONE && - tux->control == TUX_CONTROL_NET) { - return; - } - - switch (tux->position) { - case TUX_UP: - g_image = g_tux_up; - break; - - case TUX_LEFT: - g_image = g_tux_left; - break; - - case TUX_RIGHT: - g_image = g_tux_right; - break; - - case TUX_DOWN: - g_image = g_tux_down; - break; - - default: - error("Unknown direction of the tux [%d]", tux->position); - assert(0); - break; - } - - if (tux->status == TUX_STATUS_DEAD) { - g_image = g_cross; - - addLayer(g_cross, tux->x - g_cross->w / 2, - (tux->y + g_cross->h / 2) - g_cross->h, - 0, 0, g_cross->w, g_cross->h, TUX_LAYER); - - return; - } - -/* - if (tux->g_name != NULL) { - addLayer(tux->g_name, - tux->x - TUX_IMG_WIDTH / 2, - (tux->y + TUX_HEIGHT / 2) - TUX_IMG_HEIGHT, - 0, 0, - tux->g_name->w, tux->g_name->h, TUX_LAYER); - } -*/ - - addLayer(g_image, tux->x - TUX_IMG_WIDTH / 2, - (tux->y + TUX_HEIGHT / 2) - TUX_IMG_HEIGHT, - (tux->frame / TUX_KEY) * TUX_IMG_WIDTH, 0, - TUX_IMG_WIDTH, TUX_IMG_HEIGHT, TUX_LAYER); -} - -/* -void drawListTux(list_t *listTux) -{ - tux_t *thisTux; - int i; - - assert(listTux != NULL); - - for(i = 0; i < listTux->count; i++) { - thisTux = (tux_t *) listTux->list[i]; - assert(thisTux != NULL); - tux_draw(thisTux); - } -} -*/ -#endif /* PUBLIC_SERVER */ - -void tux_replace_id(tux_t *tux, int id) -{ - id_replace(tux->id, id); - tux->id = id; -} - -static void timer_spawnTux(void *p) -{ - tux_t *tux; - arena_t *arena; - int x, y; - int id; - - id = *((int *) p); - free(p); - - arena = arena_get_current(); - tux = space_get_object_id(arena->spaceTux, id); - - if (tux == NULL) { - return; - } - - tux->status = TUX_STATUS_ALIVE; - arena_find_free_space(arena_get_current(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - space_move_object(arena_get_current()->spaceTux, tux, x, y); - tux->gun = GUN_SIMPLE; - - item_add_new_item(arena->spaceItem, ID_UNKNOWN); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void timer_tuxCanShot(void *p) -{ - tux_t *tux; - int id; - - id = *((int *) p); - free(p); - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux == NULL) { - return; - } - - tux->isCanShot = TRUE; -} - -static void timer_tuxCanSwitchGun(void *p) -{ - tux_t *tux; - int id; - - id = *((int *) p); - free(p); - - tux = space_get_object_id(arena_get_current()->spaceTux, id); - - if (tux == NULL) { - return; - } - - tux->isCanSwitchGun = TRUE; -} - -void tux_event_tux_is_dead(tux_t *tux) -{ - if (tux->status == TUX_STATUS_DEAD) { - return; - } - - world_inc_round(); - -#ifndef NO_SOUND - sound_play("dead", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - - tux->status = TUX_STATUS_DEAD; - memset(tux->shot, 0, sizeof(int) * GUN_COUNT); - tux->gun = GUN_SIMPLE; - tux->shot[tux->gun] = GUN_MAX_SHOT; - - tux->bonus = BONUS_NONE; - tux->bonus_time = 0; - tux->pickup_time = 0; - - tux->isCanShot = TRUE; - tux->isCanSwitchGun = TRUE; - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, - timer_spawnTux, newInt(tux->id), TUX_TIME_SPAWN); - } - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_kill_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void tux_event_tux_is_deadWIthShot(tux_t *tux, shot_t *shot) -{ - if (shot->author_id == tux->id) { - tux->score--; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - - if (shot->author_id != tux->id) { - tux_t *author; - - author = space_get_object_id(arena_get_current()->spaceTux, shot->author_id); - - if (author != NULL) { - author->score++; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, author); - } - } - - } - - tux_event_tux_is_dead(tux); -} - -void tux_teleport(tux_t *tux) -{ - int x, y; - -#ifndef NO_SOUND - sound_play("teleport", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - arena_find_free_space(arena_get_current(), &x, &y, TUX_WIDTH, TUX_HEIGHT); - space_move_object(arena_get_current()->spaceTux, tux, x, y); - /*tux_set_proportion(tux, x, y);*/ - } - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } -} - -static void bombBallExplosion(shot_t *shot) -{ - item_t *item; - int x, y; - - x = (shot->x + shot->w / 2) - ITEM_BIG_EXPLOSION_WIDTH / 2; - y = (shot->y + shot->h / 2) - ITEM_BIG_EXPLOSION_HEIGHT / 2; - - item = item_new(x, y, ITEM_BIG_EXPLOSION, shot->author_id); - - space_add(arena_get_current()->spaceItem, item); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - /*proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id);*/ - proto_send_additem_server(PROTO_SEND_ALL, NULL, item); - } - - /*space_del_with_item(arena_get_current()->spaceShot, shot, shot_destroy);*/ -} - -static void action_tux(space_t *space, tux_t *tux, shot_t *shot) -{ - if (tux->status == TUX_STATUS_ALIVE) { - if (shot->author_id == tux->id && shot->isCanKillAuthor == FALSE) { - return; - } - - if (tux->bonus == BONUS_TELEPORT) { - tux_teleport(tux); - return; - } - - if (shot->gun == GUN_BOMBBALL) { - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - bombBallExplosion(shot); - shot->del = TRUE; - } - - return; - } - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - tux_event_tux_is_deadWIthShot(tux, shot); - } - } - - shot->del = TRUE; - -/* - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); - } - - space_del_with_item(arena->spaceShot, shot, shot_destroy); -*/ -} - -static void action_shot(space_t *space, shot_t *shot, space_t *spaceTux) -{ - space_action_from_location(spaceTux, action_tux, shot, shot->x, shot->y, shot->w, shot->h); - - if (shot->del == TRUE) { - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); - } - - space_del_with_item(space, shot, shot_destroy); - } -} - -void tux_conflict_woth_shot(arena_t *arena) -{ - /* just for a test */ - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - space_action(arena->spaceShot, action_shot, arena->spaceTux); -} - -void moveTux(tux_t *tux, int n) -{ - int px = 0, py = 0; /* no warnings */ - int zal_x, zal_y; - int new_x, new_y; - int w, h; - arena_t *arena; - - assert(tux != NULL); - - arena = arena_get_current(); - /*interval();*/ - - if (tux->position != n) { - tux->position = n; - return; - } - - if ((n == TUX_LEFT && tux->x - TUX_STEP <= 0) || - (n == TUX_RIGHT && tux->x + TUX_STEP >= arena->w) || - (n == TUX_UP && tux->y - TUX_STEP <= 0) || - (n == TUX_DOWN && tux->y + TUX_STEP >= arena->h)) { - return; - } - - tux_get_proportion(tux, &zal_x, &zal_y, &w, &h); - - tux_get_course(tux->position, &px, &py); - - new_x = zal_x; - new_y = zal_y; - - new_x += px * TUX_STEP; - new_y += py * TUX_STEP; - - if (tux->bonus == BONUS_SPEED) { - new_x += px * TUX_STEP; - new_y += py * TUX_STEP; - } - - if (tux->bonus != BONUS_GHOST && - (space_is_conflict_with_object_but(arena->spaceTux, new_x, new_y, w, h, tux) || - module_is_conflict(new_x, new_y, w, h))) { - tux_set_proportion(tux, zal_x, zal_y); - return; - } - - tux_set_proportion(tux, zal_x, zal_y); - space_move_object(arena->spaceTux, tux, new_x, new_y); - item_tux_give_list_item(tux, arena_get_current()->spaceItem); - tux->frame++; - - if (tux->frame == TUX_KEY * TUX_MAX_ANIMATION_FRAME) { - tux->frame = 0; - } -} - -void switchTuxGun(tux_t *tux) -{ - int i; - - if (tux->control != TUX_CONTROL_NET && tux->isCanSwitchGun == FALSE) { - return; - } - - for (i = tux->gun + 1; i < GUN_COUNT; i++) { - if (tux->shot[i] > 0) { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, - timer_tuxCanSwitchGun, newInt(tux->id), - TUX_TIME_CAN_SWITCH_GUN); -#ifndef NO_SOUND - sound_play("switch_gun", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - return; - } - } - - for (i = 0; i < GUN_COUNT; i++) { - if (tux->shot[i] > 0) { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, - timer_tuxCanSwitchGun, newInt(tux->id), - TUX_TIME_CAN_SWITCH_GUN); - -#ifndef NO_SOUND - sound_play("switch_gun", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - return; - } - } -} - -void shotTux(tux_t *tux) -{ - assert(tux != NULL); - - if (tux->isCanShot == FALSE || tux->shot[tux->gun] == 0) { - return; - } - -#ifndef NO_SOUND - sound_play("switch_gun", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - - gun_shot(tux); - - tux->isCanShot = FALSE; - - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, timer_tuxCanShot, - newInt(tux->id), TUX_TIME_CAN_SHOT); - - if (tux->shot[tux->gun] == 0) { - switchTuxGun(tux); - } -} - -void tux_action(tux_t *tux, int action) -{ - if (tux->status == TUX_STATUS_DEAD) { - return; - } - - switch (action) { - case TUX_UP: - case TUX_LEFT: - case TUX_RIGHT: - case TUX_DOWN: - moveTux(tux, action); - break; - - case TUX_SHOT: - shotTux(tux); - break; - - case TUX_SWITCH_GUN: - switchTuxGun(tux); - break; - } -} - -static void pickUpGun(tux_t *tux) -{ - if (tux_is_any_gun(tux) == FALSE) { - tux->gun = GUN_SIMPLE; - - if (tux->pickup_time < TUX_MAX_PICKUP) { - tux->pickup_time++; - } - - if (tux->pickup_time == TUX_MAX_PICKUP && net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { -#ifndef NO_SOUND - sound_play("switch_gun", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - - tux->gun = GUN_SIMPLE; - tux->shot[tux->gun] = GUN_MAX_SHOT; - tux->pickup_time = 0; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - } -} - -static void eventBonus(tux_t *tux) -{ - if (tux->bonus != BONUS_NONE) { - if (tux->bonus_time > 0) { - tux->bonus_time--; - } - - if (tux->bonus_time == 0 && net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - if (tux->bonus == BONUS_GHOST) { - int x, y, w, h; - tux_get_proportion(tux, &x, &y, &w, &h); - - tux->bonus = BONUS_NONE; - - if (/*isConflictTuxWithListTux(tux, arena_get_current()->listTux) ||*/ - module_is_conflict(x, y, w, h)) { - tux->bonus = BONUS_GHOST; - return; - } - } - - tux->bonus = BONUS_NONE; - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux); - } - } - } -} - -void tux_event(tux_t *tux) -{ - arena_t *arena; - - arena = arena_get_current(); - -#ifndef PUBLIC_SERVER - world_tux_control(tux); -#endif /* PUBLIC_SERVER */ - pickUpGun(tux); - eventBonus(tux); - item_tux_give_list_item(tux, arena->spaceItem); -} - -/* -void eventListTux(list_t *listTux) -{ - tux_t *thisTux; - int i; - - assert(listTux != NULL); - - for (i = 0; i < listTux->count; i++) { - thisTux = (tux_t *) listTux->list[i]; - assert(thisTux != NULL); - tux_event(thisTux); - } -} -*/ - -void tux_get_proportion(tux_t *tux, int *x, int *y, int *w, int *h) -{ - assert(tux != NULL); - - if (x != NULL) { - *x = tux->x - TUX_WIDTH / 2; - } - - if (y != NULL) { - *y = tux->y - TUX_HEIGHT / 2; - } - - if (w != NULL) { - *w = TUX_WIDTH; - } - - if (h != NULL) { - *h = TUX_HEIGHT; - } -} - -void tux_set_proportion(tux_t *tux, int x, int y) -{ - assert(tux != NULL); - - tux->x = x + TUX_WIDTH / 2; - tux->y = y + TUX_WIDTH / 2; -} - -void tux_get_status(void *p, int *id, int *x, int *y, int *w, int *h) -{ - tux_t *tux; - - tux = p; - *id = tux->id; - tux_get_proportion(tux, x, y, w, h); -} - -void tux_set_status(void *p, int x, int y, int w, int h) -{ - tux_t *tux; - - tux = p; - tux_set_proportion(tux, x, y); -} - -static void action_checkMine(space_t *space, item_t *item, tux_t *tux) -{ - if (item->type == ITEM_MINE && item->author_id == tux->id) { - item->author_id = ID_UNKNOWN; - } -} - -void tux_destroy(tux_t *tux) -{ - arena_t *arena; - - arena = arena_get_current(); - - assert(tux != NULL); - - id_del(tux->id); - - space_action(arena->spaceItem, action_checkMine, tux); - - free(tux); -} - -void tux_quit() -{ - isTuxInit = FALSE; -} diff --git a/src/base/tux.h b/src/base/tux.h deleted file mode 100644 index 9258a78..0000000 --- a/src/base/tux.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef TUX_H -#define TUX_H - -#include "main.h" -#include "list.h" -#include "arena.h" - -#define TUX_STEP 7 -#define TUX_LAYER 0 - -#define TUX_MAX_ANIMATION_FRAME 10 -#define TUX_KEY 1 - -#define TUX_LOCATE_UNKNOWN -100 - -#define TUX_MAX_PICKUP 50 -#define TUX_MAX_BONUS 500 - -#define TUX_TIME_SPAWN 2000 -#define TUX_TIME_CAN_SHOT 500 -#define TUX_TIME_CAN_SWITCH_GUN 100 - -#define TUX_STATUS_ALIVE 0 -#define TUX_STATUS_DEAD 1 -#define TUX_STATUS_PAUZED 2 - -#define TUX_CONTROL_NONE 0 -#define TUX_CONTROL_KEYBOARD_LEFT 1 -#define TUX_CONTROL_KEYBOARD_RIGHT 2 -#define TUX_CONTROL_NET 3 -#define TUX_CONTROL_AI 4 - -#define TUX_WIDTH 30 -#define TUX_HEIGHT 30 - -#define TUX_IMG_WIDTH 40 -#define TUX_IMG_HEIGHT 54 - -#define TUX_UP 8 -#define TUX_LEFT 4 -#define TUX_RIGHT 6 -#define TUX_DOWN 2 -#define POSITION_UNKNOWN -1 - -#define TUX_SHOT 5 -#define TUX_SWITCH_GUN 0 - - -#define GUN_NONE -1 - -#define GUN_COUNT 7 -#define GUN_MAX_SHOT 5 - -#define BONUS_COUNT 6 -#define ITEM_COUNT 16 - - -#define SHOT_CHECK_AREA 25 - -#define GUN_SIMPLE 0 -#define GUN_DUAL_SIMPLE 1 -#define GUN_SCATTER 2 -#define GUN_TOMMY 3 -#define GUN_LASSER 4 -#define GUN_MINE 5 -#define GUN_BOMBBALL 6 - -#define ITEM_MINE 7 -#define ITEM_EXPLOSION 8 -#define ITEM_BIG_EXPLOSION 9 - -#define BONUS_NONE -1 -#define BONUS_SPEED 10 -#define BONUS_SHOT 11 -#define BONUS_TELEPORT 12 -#define BONUS_GHOST 13 -#define BONUS_4X 14 -#define BONUS_HIDDEN 15 - -typedef struct tux_struct { - int id; - - int x; - int y; - - char name[STR_NAME_SIZE]; - - int status; - int control; - int position; - int score; - - int gun; - int shot[GUN_COUNT]; - int round; - int bonus; - - int bonus_time; - int pickup_time; - - bool_t isCanShot; - bool_t isCanSwitchGun; - - int frame; - - void *client; -} tux_t; - -extern bool_t tux_is_inicialized(); -extern void tux_init(); -extern tux_t *tux_new(); -extern void tux_set_name(tux_t *tux, char *name); -extern bool_t tux_is_any_gun(tux_t *tux); -extern void tux_get_course(int n, int *x, int *y); -extern void tux_draw(tux_t *tux); -/*extern void drawListTux(list_t *listTux);*/ -extern void tux_event_tux_is_dead(tux_t *tux); -/*extern tux_t *isConflictWithListTux(list_t *listTux, int x, int y, int w, int h);*/ -/*extern int isConflictTuxWithListTux(tux_t *tux, list_t *listTux);*/ -extern void tux_conflict_woth_shot(arena_t *arena); -/*extern void eventConflictTuxWithTeleport(list_t *listTux, list_t *listTeleport);*/ -extern void tux_teleport(tux_t *tux); -extern void tux_action(tux_t *tux, int action); -extern void tux_event(tux_t *tux); -/*extern void eventListTux(list_t *listTux);*/ -/*extern tux_t *getTuxID(list_t *listTux, int id);*/ -extern void tux_get_proportion(tux_t *tux, int *x, int *y, int *w, int *h); -extern void tux_replace_id(tux_t *tux, int id); -extern void tux_set_proportion(tux_t *tux, int x, int y); -extern void tux_get_status(void *p, int *id, int *x, int *y, int *w, int *h); -extern void tux_set_status(void *p, int x, int y, int w, int h); -extern void tux_destroy(tux_t *tux); -extern void tux_quit(); - -#endif /* TUX_H */ diff --git a/src/base/udp_server.c b/src/base/udp_server.c deleted file mode 100644 index aa24c46..0000000 --- a/src/base/udp_server.c +++ /dev/null @@ -1,244 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <sys/types.h> - -#include <unistd.h> -#include <fcntl.h> -#include <sys/time.h> - -#ifndef __WIN32__ -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/select.h> -#else /* __WIN32__ */ -#include <io.h> -#include <winsock2.h> -#endif /* __WIN32__ */ - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "proto.h" -#include "server.h" -#include "myTimer.h" -#include "arena.h" -#include "net_multiplayer.h" -#include "checkFront.h" -#include "protect.h" -#include "index.h" - -#ifndef PUBLIC_SERVER -#include "world.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#include "highScore.h" -#include "log.h" -#endif /* PUBLIC_SERVER */ - -#include "udp.h" -#include "udp_server.h" -#include "serverSelect.h" - -static sock_udp_t *sock_server_udp; -static sock_udp_t *sock_server_udp_second; - -client_t *server_udp_new_client(sock_udp_t *sock_udp) -{ - client_t *new; - - assert(sock_udp != NULL); - - new = server_new_any_client(); - new->type = CLIENT_TYPE_UDP; - new->socket_udp = sock_udp; - -#ifdef PUBLIC_SERVER - char str_log[STR_LOG_SIZE]; - char str_ip[STR_IP_SIZE]; - - sock_udp_get_ip(sock_udp, str_ip, STR_IP_SIZE); - sprintf(str_log, _("New player [%s]:%d connected"), str_ip, sock_udp_get_port(sock_udp)); - log_add(LOG_INF, str_log); -#endif /* PUBLIC_SERVER */ - - return new; -} - -void server_udp_destroy_client(client_t *p) -{ - check_front_event(p); - -#ifdef PUBLIC_SERVER - char str_log[STR_LOG_SIZE]; - char str_ip[STR_IP_SIZE]; - - sock_udp_get_ip(p->socket_udp, str_ip, STR_IP_SIZE); - sprintf(str_log, _("Player [%s]:%d disconnected"), str_ip, sock_udp_get_port(p->socket_udp)); - log_add(LOG_INF, str_log); -#endif /* PUBLIC_SERVER */ - - sock_udp_destroy(p->socket_udp); - - server_destroy_any_client(p); -} - -int server_udp_init(char *ip4, char *ip6, int port) -{ - int ret; - - ret = 0; - - if (ip4 != NULL) { - sock_server_udp = sock_udp_bind(ip4, port); - - if (sock_server_udp != NULL) { - ret++; - debug("Starting server on [%s]:%d", ip4, port); - } else { - debug("Unable to start server on [%s]:%d", ip4, port); - } - } - - if (ip6 != NULL) { - sock_server_udp_second = sock_udp_bind(ip6, port); - - if (sock_server_udp_second != NULL) { - ret++; - debug("Starting server on [%s]:%d", ip6, port); - } else { - debug("Unable to start server on [%s]:%d", ip6, port); - } - } - - return ret; -} - -static void eventCreateNewUdpClient(sock_udp_t *socket_udp) -{ - list_t *listClient; - client_t *client; - - assert(socket_udp != NULL); - - listClient = server_get_list_clients(); - - client = server_udp_new_client(socket_udp); - list_add(listClient, client); -} - -static client_t *findUdpClient(sock_udp_t *sock_udp) -{ - list_t *listClient; - int port; - int i; - - port = sock_udp_get_port(sock_udp); - listClient = server_get_list_clients(); - - for (i = 0; i < listClient->count; i++) { - client_t *client; - - client = (client_t *) listClient->list[i]; - - if (client->type == CLIENT_TYPE_UDP && - sock_udp_get_port(client->socket_udp) == port) { - return client; - } - } - - return NULL; -} - -static void client_eventUdpSelect(sock_udp_t *sock_server) -{ - sock_udp_t *sock_client; - client_t *client; - char listRecvMsg[STR_SIZE]; - bool_t isCreateNewClient; - int ret; - - assert(sock_server != NULL); - - sock_client = sock_udp_new(); - isCreateNewClient = FALSE; - - memset(listRecvMsg, 0, STR_SIZE); - - ret = sock_udp_read(sock_server, sock_client, listRecvMsg, STR_SIZE - 1); - - client = findUdpClient(sock_client); - - if (client == NULL) { - eventCreateNewUdpClient(sock_client); - client = findUdpClient(sock_client); - isCreateNewClient = TRUE; - } - - if (client == NULL) { - error("UDP client not found"); - return; - } - - if (isCreateNewClient == FALSE) { - sock_udp_destroy(sock_client); - } - - if (ret <= 0) { - client->status = NET_STATUS_ZOMBIE; - return; - } - - /*printf("add packet >>%s<<\n", listRecvMsg);*/ - list_add(client->listRecvMsg, strdup(listRecvMsg)); -} - -void server_udp_set_select() -{ - if (sock_server_udp != NULL) { - select_add_sock_for_read(sock_server_udp->sock); - } - - if (sock_server_udp_second != NULL) { - select_add_sock_for_read(sock_server_udp_second->sock); - } -} - -int server_udp_select_sock() -{ - int count; - - count = 0; - - if (sock_server_udp != NULL) { - if (select_is_change_sock_for_read(sock_server_udp->sock)) { - client_eventUdpSelect(sock_server_udp); - count++; - } - } - - if (sock_server_udp_second != NULL) { - if (select_is_change_sock_for_read(sock_server_udp_second->sock)) { - client_eventUdpSelect(sock_server_udp_second); - count++; - } - } - - return count; -} - -void server_udp_quit() -{ - debug("Shutting down UDP"); - - if (sock_server_udp != NULL) { - debug("Closing IPv4 [port %d]", sock_udp_get_port(sock_server_udp)); - sock_udp_close(sock_server_udp); - } - - if (sock_server_udp_second != NULL) { - debug("Closing IPv6 [port %d]", sock_udp_get_port(sock_server_udp_second)); - sock_udp_close(sock_server_udp_second); - } -} diff --git a/src/base/udp_server.h b/src/base/udp_server.h deleted file mode 100644 index 0d4c1d9..0000000 --- a/src/base/udp_server.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef SERVER_UDP_H -#define SERVER_UDP_H - -#include "server.h" -#include "udp.h" - -extern client_t *server_udp_new_client(sock_udp_t *sock_udp); -extern void server_udp_destroy_client(client_t *p); -extern int server_udp_init(char *ip4, char *ip6, int port); -extern void server_udp_set_select(); -extern int server_udp_select_sock(); -extern void server_udp_quit(); - -#endif /* SERVER_UDP_H */ diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/client/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/client/chat.c b/src/client/chat.c deleted file mode 100644 index d68edb8..0000000 --- a/src/client/chat.c +++ /dev/null @@ -1,295 +0,0 @@ -#include <stdlib.h> -#include <ctype.h> -#include <assert.h> -#include <SDL.h> - -#include "main.h" -#include "list.h" -#include "myTimer.h" -#include "image.h" -#include "serverSendMsg.h" - -#include "net_multiplayer.h" -#include "world.h" -#include "proto.h" -#include "interface.h" -#include "chat.h" -#include "font.h" -#include "keyboardBuffer.h" -#include "hotKey.h" - -static image_t *g_chat; - -static list_t *listText; - -static char line[STR_SIZE]; - -static int line_time, line_atime; -static int timeBlick; - -static bool_t chat_active; -static bool_t receivedNewMsg; - -static void hotkey_chat_enter(); -static void hotkey_chat_enter(); -static void chat_eventDisable(); - -static void hotkey_chat_esc() -{ - /* - printf("*** hotkey_chat_esc\n"); - hot_key_unregister(SDLK_ESCAPE); - */ - chat_eventDisable(); -} - -static void hotkey_chat_enter() -{ - if (chat_active == TRUE) { - return; - } - - chat_active = TRUE; - receivedNewMsg = FALSE; - - hot_key_disable(SDLK_RETURN); - hot_key_disable(SDLK_p); - hot_key_register(SDLK_ESCAPE, hotkey_chat_esc); - - interface_enable_keyboard_buffer(); - keyboard_buffer_clear(); -} - -void chat_init() -{ - g_chat = image_add("chat.png", IMAGE_NO_ALPHA, "chat", IMAGE_GROUP_USER); - - listText = list_new(); - strcpy(line, ""); - chat_active = FALSE; - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_NONE) { - hot_key_register(SDLK_RETURN, hotkey_chat_enter); - } - - receivedNewMsg = FALSE; - line_time = 0; - line_atime = 0; - timeBlick = 0; -} - -void chat_draw() -{ - char str[STR_SIZE]; - int i; - - if (chat_active == FALSE) { - return; - } - - image_draw(g_chat, CHAT_LOCATION_X, CHAT_LOCATION_Y, - 0, 0, - CHAT_SIZE_X, CHAT_SIZE_Y); - - for (i = 0; i < listText->count; i++) { - char *s; - - s = (char *) listText->list[i]; - - font_drawMaxSize(s, CHAT_LOCATION_X + 5, CHAT_LOCATION_Y + 5 + i * 20, - CHAT_SIZE_X - 10, 20, COLOR_WHITE); - } - - strcpy(str, line); - - if (timeBlick > CHAT_TIME_BLICK_CURSOR / 2) { - strcat(str, "\f"); - } - - timeBlick++; - - if (timeBlick == CHAT_TIME_BLICK_CURSOR) { - timeBlick = 0; - } - - font_draw(str, CHAT_LOCATION_X + 5, (CHAT_LOCATION_Y + 5) + (CHAT_MAX_LINES * 20), COLOR_WHITE); -} - -static void processMessageKey(SDL_keysym keysym) -{ - int w, h; - int len; - unsigned n; - - char shift_map[] = { - '-', '_', - '=', '+', - '[', '{', - ']', '}', - ';', ':', - '/', '\"', - '\\', '|', - ',', '<', - '.', '>', - '/', '?', - ')', '(' - }; - - if (line_atime < 100) { - line_atime++; - } - - len = strlen(line); - font_text_size(line, &w, &h); - - /* processing of backspace */ - if (keysym.sym == SDLK_BACKSPACE && len > 0) { - line[len - 1] = '\0'; - return; - } - - /* end of line */ - if (w > CHAT_SIZE_X - 20) { - return; - } - - /* processing of printable chars but not letters */ - if ((keysym.sym >= SDLK_SPACE && keysym.sym <= SDLK_AT) || - (keysym.sym >= SDLK_LEFTBRACKET && keysym.sym <= SDLK_BACKQUOTE)) { - /* here would be much better to put 'strcat' as 'line' is expected - * to be full of '0' up to its end - */ - char c = keysym.sym; - - if (keysym.mod & KMOD_RSHIFT) { - for (n = 0; n < sizeof (shift_map); n += 2) { - if (c == shift_map[n]) { - c = shift_map[n+1]; - break; - } - } - } - - line[len] = c; - return; - } - - /* processing of letters */ - if (keysym.sym >= SDLK_a && keysym.sym <= SDLK_z) { - char c = keysym.sym; - - if (keysym.mod & KMOD_SHIFT) { - c = toupper(c); - } - - /* here would be much better to put 'strcat' as 'line' is expected - * to be full of '0' up to its end - */ - line[len] = c; - } - - return; -} - -static void sendNewMessage() -{ - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - proto_send_chat_client(line); - } - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - char out[STR_PROTO_SIZE]; - - snprintf(out, STR_PROTO_SIZE, "chat %s:%s\n", - world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT)->name, - line); - - proto_send_chat_server(PROTO_SEND_ALL, NULL, out); - } -} - -static void chat_eventDisable() -{ - chat_active = FALSE; - memset(line, '\0', STR_SIZE); - - hot_key_enable(SDLK_RETURN); - hot_key_enable(SDLK_p); - hot_key_unregister(SDLK_ESCAPE); - - interface_disable_keyboard_buffer(); - keyboard_buffer_clear(); -} - -static void chat_eventEnable() -{ - /* The chat window is displayed. It is needed to process all keys in the buffer - * but when ENTER is pressed the chat row is sent to the server - */ - - while (keyboard_buffer_is_any_key() == TRUE) { - SDL_keysym key; - key = keyboard_buffer_pop(); - - if (key.sym == SDLK_RETURN) { - if (strcmp(line, "") != 0) { - /* the chat row is already written - it is needed to send it */ - sendNewMessage(); - memset(line, '\0', STR_SIZE); - continue; /* continue with other keys */ - } else { - /* the chat row is empty - it is needed to turn off the chat window */ - /*chat_eventDisable();*/ - /* turn off key catching into the buffer and clear the buffer */ - /*interface_disable_keyboard_buffer();*/ - /*keyboard_buffer_clear();*/ - } - } - - processMessageKey(key); - } -} - -/** - * Processing of chat 'events' - */ -void chat_event() -{ - if (chat_is_active()) { - chat_eventEnable(); - } -} - -bool_t chat_is_active() -{ - return chat_active; -} - -bool_t chat_is_recived_new_msg() -{ - return receivedNewMsg; -} - -void chat_add(char *s) -{ - list_add(listText, strdup(s)); - - if (listText->count > CHAT_MAX_LINES) { - list_del_item(listText, 0, free); - } - - if (chat_active == FALSE) { - receivedNewMsg = TRUE; - } -} - -void chat_quit() -{ - assert(listText != NULL); - - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_NONE) { - hot_key_unregister(SDLK_RETURN); - } - - list_destroy_item(listText, free); -} diff --git a/src/client/chat.h b/src/client/chat.h deleted file mode 100644 index 5427f60..0000000 --- a/src/client/chat.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef CHAT_H -#define CHAT_H - -#define CHAT_MAX_LINES 10 -#define CHAT_TIME_MULTIPLE 10 -#define CHAT_TIME_BLICK_CURSOR 20 -#define CHAT_LINE_WIDTH 320 - -#define CHAT_SIZE_X 320 -#define CHAT_SIZE_Y 240 - -#define CHAT_LOCATION_X (WINDOW_SIZE_X / 2 - CHAT_SIZE_X / 2) -#define CHAT_LOCATION_Y (WINDOW_SIZE_Y / 2 - CHAT_SIZE_Y / 2) - -extern void chat_init(); -extern void chat_draw(); -extern void chat_event(); -extern void chat_add(char *s); -extern bool_t chat_is_active(); -extern bool_t chat_is_recived_new_msg(); -extern void chat_quit(); - -#endif /* CHAT_H */ diff --git a/src/client/client.c b/src/client/client.c deleted file mode 100644 index 054b3ea..0000000 --- a/src/client/client.c +++ /dev/null @@ -1,338 +0,0 @@ -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> - -#include <fcntl.h> -#include <sys/types.h> -#include <sys/time.h> - -#ifndef __WIN32__ -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/select.h> -#else /* __WIN32__ */ -#include <io.h> -#include <winsock2.h> -#endif /* __WIN32__ */ - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "proto.h" - -#include "client.h" - -#include "analyze.h" -#include "world.h" -#include "buffer.h" - -#include "udp.h" - -static sock_udp_t *sock_server_udp; - -static list_t *listRecvMsg; - -static buffer_t *clientRecvBuffer; -static buffer_t *clientSendBuffer; - -static my_time_t lastPing; -static my_time_t lastPingServerAlive; - -#ifdef SUPPORT_TRAFFIC -static my_time_t lastTraffic; -static int traffic_down; -static int traffic_up; -#endif /* SUPPORT_TRAFFIC */ - -typedef struct proto_cmd_client_struct { - char *name; - int len; - void (*fce_proto) (char *msg); -} proto_cmd_client_t; - -static proto_cmd_client_t proto_cmd_list[] = { - {.name = "error",.len = 5,.fce_proto = proto_recv_error_client}, - {.name = "init",.len = 4,.fce_proto = proto_recv_init_client}, - {.name = "event",.len = 5,.fce_proto = proto_recv_event_client}, - {.name = "newtux",.len = 6,.fce_proto = proto_recv_newtux_client}, - {.name = "del",.len = 3,.fce_proto = proto_recv_del_client}, - {.name = "additem",.len = 7,.fce_proto = proto_recv_additem_client}, - {.name = "shot",.len = 4,.fce_proto = proto_recv_shot_client}, - {.name = "kill",.len = 4,.fce_proto = proto_recv_kill_client}, - {.name = "module",.len = 6,.fce_proto = proto_recv_module_client}, - {.name = "chat",.len = 4,.fce_proto = proto_recv_chat_client}, - {.name = "ping",.len = 4,.fce_proto = proto_recv_ping_client}, - {.name = "end",.len = 3,.fce_proto = proto_recv_end_client}, - {.name = "",.len = 0,.fce_proto = NULL} -}; - -static proto_cmd_client_t *findCmdProto(char *msg) -{ - int len; - int i; - - len = strlen(msg); - - for (i = 0; proto_cmd_list[i].len != 0; i++) { - proto_cmd_client_t *thisCmd; - - thisCmd = &proto_cmd_list[i]; - - if (len >= thisCmd->len && - strncmp(msg, thisCmd->name, thisCmd->len) == 0) { - return thisCmd; - } - } - - return NULL; -} - -static int initUdpClient(char *ip, int port) -{ - sock_server_udp = sock_udp_connect(ip, port); - - if (sock_server_udp == NULL) { - return -1; - } - - debug("Connected to game server [%s]:[%d]", ip, port); - - return 0; -} - -int client_init(char *ip, int port) -{ - char name[STR_NAME_SIZE]; - int ret; - - listRecvMsg = list_new(); - lastPing = timer_get_current_time(); - lastPingServerAlive = timer_get_current_time(); - -#ifdef SUPPORT_TRAFFIC - lastTraffic = timer_get_current_time(); - traffic_down = 0; - traffic_up = 0; -#endif /* SUPPORT_TRAFFIC */ - - clientRecvBuffer = buffer_new(CLIENT_BUFFER_LIMIT); - clientSendBuffer = buffer_new(CLIENT_BUFFER_LIMIT); - - ret = initUdpClient(ip, port); - - if (ret < 0) { - return -1; - } - - public_server_get_setting_name_right(name); - proto_send_hello_client(name); - - return 0; -} - -static void errorWithServer() -{ - error("Game server is not responding"); - analyze_set_msg(_("Unable to connect to the game server")); - world_do_end(); -} - -void client_send(char *msg) -{ - int ret; - - assert(msg != NULL); - - ret = -1; - - if (isParamFlag("--send")) { - debug("Sending data: %s", msg); - } - -#ifdef SUPPORT_TRAFFIC - traffic_up += strlen(msg); -#endif /* SUPPORT_TRAFFIC */ - - if (sock_server_udp != NULL) { - ret = sock_udp_write(sock_server_udp, sock_server_udp, msg, strlen(msg)); - } - - if (ret < 0) { - errorWithServer(); - return; - } -} - -static int server_eventSelect() -{ - char buffer[STR_PROTO_SIZE]; - int ret; - - memset(buffer, 0, STR_PROTO_SIZE); - ret = -1; - - if (sock_server_udp != NULL) { - ret = sock_udp_read(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE - 1); - } - - if (ret < 0) { - errorWithServer(); - return ret; - } - -#ifdef SUPPORT_TRAFFIC - traffic_down += ret; -#endif /* SUPPORT_TRAFFIC */ - - list_add(listRecvMsg, strdup(buffer)); - - return ret; -} - -static void client_eventWorkRecvList() -{ - proto_cmd_client_t *protoCmd; - char *line; - int i; - - /* processing of an event received from a game server */ - - assert(listRecvMsg != NULL); - - for (i = 0; i < listRecvMsg->count; i++) { - line = (char *) listRecvMsg->list[i]; - protoCmd = findCmdProto(line); - - if (isParamFlag("--recv")) { - debug("Recieved data: %s", line); - } - - if (protoCmd != NULL) { - protoCmd->fce_proto(line); - lastPingServerAlive = timer_get_current_time(); - } - } - - list_destroy_item(listRecvMsg, free); - listRecvMsg = list_new(); -} - -static void eventPingServer() -{ - my_time_t currentTime; - - currentTime = timer_get_current_time(); - - if (currentTime - lastPing > CLIENT_TIMEOUT) { - proto_send_ping_client(); - lastPing = timer_get_current_time(); - } -} - -static bool_t isServerAlive() -{ - my_time_t currentTime; - - currentTime = timer_get_current_time(); - - if (currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE) { - return FALSE; - } - - return TRUE; -} - -static void selectClientSocket() -{ - fd_set readfds; - struct timeval tv; - int max_fd; - int sock; - bool_t isNext; - - max_fd = 0; - sock = 0; - - if (sock_server_udp != NULL) { - if (isServerAlive() == FALSE) { - errorWithServer(); - return; - } - } - - do { - isNext = FALSE; - - tv.tv_sec = 0; - tv.tv_usec = 0; - - FD_ZERO(&readfds); - - if (sock_server_udp != NULL) { - sock = sock_server_udp->sock; - } - - FD_SET(sock, &readfds); - max_fd = sock; - select(max_fd + 1, &readfds, (fd_set *) NULL, (fd_set *) NULL, &tv); - - if (FD_ISSET(sock, &readfds)) { - if (server_eventSelect() > 0) { - isNext = TRUE; - } - } - - } while (isNext == TRUE); -} - -#ifdef SUPPORT_TRAFFIC -static void eventTraffic() -{ - my_time_t currentTime; - - currentTime = timer_get_current_time(); - - if (currentTime - lastTraffic > 5000) { - lastTraffic = currentTime; - - debug("Traffic: down [%d]/up [%d]", traffic_down, traffic_up); - - traffic_down = 0; - traffic_up = 0; - } -} -#endif /* SUPPORT_TRAFFIC */ - -void client_event() -{ -#ifdef SUPPORT_TRAFFIC - eventTraffic(); -#endif /* SUPPORT_TRAFFIC */ - - eventPingServer(); - selectClientSocket(); - client_eventWorkRecvList(); -} - -static void quitUdpClient() -{ - assert(sock_server_udp != NULL); - sock_udp_close(sock_server_udp); - - debug("Closing connection to game server"); -} - -void client_quit() -{ - proto_send_end_client(); - - assert(listRecvMsg != NULL); - - list_destroy_item(listRecvMsg, free); - buffer_destroy(clientRecvBuffer); - buffer_destroy(clientSendBuffer); - - if (sock_server_udp != NULL) { - quitUdpClient(); - } -} diff --git a/src/client/client.h b/src/client/client.h deleted file mode 100644 index ce41ffb..0000000 --- a/src/client/client.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef MY_CLIENT_H -#define MY_CLIENT_H - -#include "main.h" - -#define CLIENT_TIMEOUT 1000 -#define SERVER_TIMEOUT_ALIVE 5000 - -#define CLIENT_BUFFER_LIMIT 4096 - -extern int client_init(char *ip, int port); -extern void client_send(char *msg); -extern void client_event(); -extern void client_quit(); - -#endif /* MY_CLIENT_H */ diff --git a/src/client/config.c b/src/client/config.c deleted file mode 100644 index 130fcdf..0000000 --- a/src/client/config.c +++ /dev/null @@ -1,227 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "homeDirector.h" -#include "textFile.h" -#include "configFile.h" -#include "config.h" - -typedef struct setting_int_struct { - int key; /* identification key */ - char *param; /* parameter of the program */ - char *var; /* variable in the config file */ - int type; /* type of the data - string/number */ - int value_int; /* numeric data */ - char *value_str; /* textual data */ -} config_t; - -#define CONFIG_TYPE_INT 1 -#define CONFIG_TYPE_STR 2 - -static config_t config_list[] = -{ - { .key=CFG_NAME_RIGHT, .var="NAME_PLAYER_RIGHT", .param="--name-right", .type=CONFIG_TYPE_STR, .value_str="name1" }, - { .key=CFG_NAME_LEFT, .var="NAME_PLAYER_LEFT", .param="--name-left", .type=CONFIG_TYPE_STR, .value_str="name2" }, - { .key=CFG_COUNT_ROUND, .var="COUNT_ROUND", .param="--count", .type=CONFIG_TYPE_INT, .value_int=15 }, - { .key=CFG_AI, .var="AI", .param="--ai", .type=CONFIG_TYPE_INT, .value_int=0 }, - { .key=CFG_MUSIC, .var="MUSIC", .param="--music", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_SOUND, .var="SOUND", .param="--sound", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_ARENA, .var="ARENA", .param="--arena", .type=CONFIG_TYPE_STR, .value_str="FAGN" }, - - { .key=CFG_GUN_DUAL_SIMPLE, .var="GUN_DUAL_SIMPLE", .param="--gun-dual-simple", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_GUN_SCATTER, .var="GUN_SCATTER", .param="--gun-scatter", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_GUN_TOMMY, .var="GUN_TOMMY", .param="--gun-tommy", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_GUN_LASSER, .var="GUN_LASSER", .param="--gun-lasser", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_GUN_MINE, .var="GUN_MINE", .param="--gun-mine", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_GUN_BOMBBALL, .var="GUN_BOMBBALL", .param="--gun-bombball", .type=CONFIG_TYPE_INT, .value_int=1 }, - - { .key=CFG_BONUS_SPEED, .var="BONUS_SPEED", .param="--bonus-speed", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_BONUS_SHOT, .var="BONUS_SHOT", .param="--bonus-shot", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_BONUS_TELEPORT, .var="BONUS_TELEPORT", .param="--bonus-teleport", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_BONUS_GHOST, .var="BONUS_GHOST", .param="--bonus-ghost", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_BONUS_4X, .var="BONUS_4X", .param="--bonus-4x", .type=CONFIG_TYPE_INT, .value_int=1 }, - { .key=CFG_BONUS_HIDDEN, .var="BONUS_HIDDEN", .param="--bonus-hidden", .type=CONFIG_TYPE_INT, .value_int=1 }, - - { .key=CFG_GAME_TYPE, .var="GAME_TYPE", .param="--game-type", .type=CONFIG_TYPE_STR, .value_str="none" }, - { .key=CFG_NET_IP, .var="NET_IP", .param="--ip", .type=CONFIG_TYPE_STR, .value_str="127.0.0.1" }, - { .key=CFG_NET_PORT, .var="NET_PORT", .param="--port", .type=CONFIG_TYPE_STR, .value_str="6800" } -}; - -static textFile_t *config_file; - -static config_t *find_config(int key) -{ - int i; - - for (i = 0; i < CONFIG_LIST_COUNT; i++) { - if (config_list[i].key == key) { - return &config_list[i]; - } - } - - return NULL; -} - -static void check_param() -{ - int i; - - for (i = 0; i < CONFIG_LIST_COUNT; i++) { - char *str; - - str = getParam(config_list[i].param); - - if (str != NULL) { - if (config_list[i].type == CONFIG_TYPE_INT) { - config_list[i].value_int = atoi(str); - } else { - if (config_list[i].value_str != NULL) { - free(config_list[i].value_str); - } - - config_list[i].value_str = strdup(str); - } - } - } -} - -int config_init() -{ - config_load(); - check_param(); - - return 0; -} - -void config_load() -{ - char path[STR_PATH_SIZE]; - char val[STR_SIZE]; - int i; - - sprintf(path, "%s%stuxanci.conf", home_director_get(), PATH_SEPARATOR); - - config_file = text_file_load(path); - - if (config_file == NULL) { - error("Unable to load config file [%s]", path); - debug("Creating config file [%s]", path); - - config_file = text_file_new(path); - } - - if (config_file == NULL) { - error("Unable to create config file [%s]", path); - return; - } - - for (i = 0; i < CONFIG_LIST_COUNT; i++) { - if (config_list[i].type == CONFIG_TYPE_INT) { - char str[STR_SIZE]; - - sprintf(str, "%d", config_list[i].value_int); - loadValueFromConfigFile(config_file, config_list[i].var, val, STR_SIZE, str); - config_list[i].value_int = atoi(val); - } else { - loadValueFromConfigFile(config_file, config_list[i].var, val, STR_SIZE, config_list[i].value_str); - config_list[i].value_str = strdup(val); - } - } -} - -void config_save() -{ - int i; - - if (config_file == NULL) { - return; - } - - for (i = 0; i < CONFIG_LIST_COUNT; i++) { - if (config_list[i].type == CONFIG_TYPE_INT) { - char str[STR_SIZE]; - - sprintf(str, "%d", config_list[i].value_int); - setValueInConfigFile(config_file, config_list[i].var, str); - } else { - setValueInConfigFile(config_file, config_list[i].var, config_list[i].value_str); - } - } - - text_file_save(config_file); -} - -int config_get_int_value(int key) -{ - config_t *tmp; - - tmp = find_config(key); - - assert(tmp != NULL); - assert(tmp->type == CONFIG_TYPE_INT); - - return tmp->value_int; -} - -char *config_get_str_value(int key) -{ - config_t *tmp; - - tmp = find_config(key); - - assert(tmp != NULL); - assert(tmp->type == CONFIG_TYPE_STR); - - return tmp->value_str; -} - -void config_set_int_value(int key, int n) -{ - config_t *tmp; - - tmp = find_config(key); - - assert(tmp != NULL); - assert(tmp->type == CONFIG_TYPE_INT); - - tmp->value_int = n; -} - -void config_set_str_value(int key, char *str) -{ - config_t *tmp; - - tmp = find_config(key); - - assert(tmp != NULL); - assert(tmp->type == CONFIG_TYPE_STR); - - if (tmp->value_str != NULL) { - free(tmp->value_str); /* has to be allocated */ - } - - tmp->value_str = strdup(str); -} - -int config_quit() -{ - int i; - - config_save(); - - if (config_file != NULL) { - text_file_destroy(config_file); - } - - for (i = 0; i < CONFIG_LIST_COUNT; i++) { - if (config_list[i].type == CONFIG_TYPE_STR && config_list[i].value_str != NULL) { - free(config_list[i].value_str); - } - } - - return 0; -} diff --git a/src/client/config.h b/src/client/config.h deleted file mode 100644 index 46e0da5..0000000 --- a/src/client/config.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define CFG_NAME_RIGHT 1 -#define CFG_NAME_LEFT 2 -#define CFG_COUNT_ROUND 3 -#define CFG_AI 4 -#define CFG_MUSIC 5 -#define CFG_SOUND 6 -#define CFG_ARENA 7 - -#define CFG_GUN_DUAL_SIMPLE 8 -#define CFG_GUN_SCATTER 9 -#define CFG_GUN_TOMMY 10 -#define CFG_GUN_LASSER 11 -#define CFG_GUN_MINE 12 -#define CFG_GUN_BOMBBALL 13 - - -#define CFG_BONUS_SPEED 14 -#define CFG_BONUS_SHOT 15 -#define CFG_BONUS_TELEPORT 16 -#define CFG_BONUS_GHOST 17 -#define CFG_BONUS_4X 18 -#define CFG_BONUS_HIDDEN 19 - -#define CFG_GAME_TYPE 20 -#define CFG_NET_IP 21 -#define CFG_NET_PORT 22 - -#define CONFIG_LIST_COUNT 22 - -extern int config_init(); -extern void config_load(); -extern void config_save(); -extern int config_get_int_value(int key); -extern char *config_get_str_value(int key); -extern void config_set_int_value(int key, int n); -extern void config_set_str_value(int key, char *str); -extern int config_quit(); - -#endif /* CONFIG_H */ diff --git a/src/client/configFile.c b/src/client/configFile.c deleted file mode 100644 index 9d6d65b..0000000 --- a/src/client/configFile.c +++ /dev/null @@ -1,152 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "configFile.h" - -int isYesOrNO(char *s) -{ - if (s[0] == 'Y' || s[0] == 'y') { - return 1; - } else { - return 0; - } -} - -char *getYesOrNo(int n) -{ - return (n != 0 ? "YES" : "NO"); -} - -int getValue(char *line, char *env, char *val, int len) -{ - char *offset_env; - char *offset_val_begin; - char *offset_val_end; - char clone_env[STR_SIZE]; - int val_len; - - strcpy(clone_env, env); - - offset_env = strstr(line, clone_env); - - if (offset_env == NULL) { - return -1; - } - - offset_val_begin = strchr(offset_env, '"'); - - if (offset_val_begin == NULL) { - return -1; - } - - offset_val_end = strchr(offset_val_begin + 1, '"'); - - if (offset_val_end == NULL) { - return -1; - } - - val_len = (int) (offset_val_end - (offset_val_begin + 1)); - - if (val_len > len - 1) { - val_len = len - 1; - } - - memset(val, 0, len); - memcpy(val, offset_val_begin + 1, val_len); - - return 0; -} - -char *setValue(char *line, char *env, char *val) -{ - char *offset_env; - char *offset_val_begin; - char *offset_val_end; - char clone_env[STR_SIZE]; - char clone[STR_SIZE]; - - /*strcpy(clone_env, " ");*/ - strcpy(clone_env, env); - - offset_env = strstr(line, clone_env); - - if (offset_env == NULL) { - return NULL; - } - - offset_val_begin = strchr(offset_env, '"'); - - if (offset_val_begin == NULL) { - return NULL; - } - - offset_val_end = strchr(offset_val_begin + 1, '"'); - - if (offset_val_end == NULL) { - return NULL; - } - - memset(clone, 0, STR_SIZE); - strncpy(clone, line, (int) ((offset_val_begin - line) + 1)); - strcat(clone, val); - strcat(clone, offset_val_end); - - return strdup(clone); -} - -int getValueInConfigFile(textFile_t * textFile, char *env, char *val, int len) -{ - int i; - char *line; - - for (i = 0; i < textFile->text->count; i++) { - line = (char *) (textFile->text->list[i]); - - if (strncmp(line, env, strlen(env)) == 0) { - return getValue(line, env, val, len); - } - } - - return -1; -} - -int setValueInConfigFile(textFile_t * textFile, char *env, char *val) -{ - int i; - char *line; - char *ret; - - for (i = 0; i < textFile->text->count; i++) { - line = (char *) (textFile->text->list[i]); - - if (strncmp(line, env, strlen(env)) == 0) { - ret = setValue(line, env, val); - - if (ret != NULL) { - list_del_item(textFile->text, i, free); - list_ins(textFile->text, i, ret); - - return 0; - } - } - } - - return -1; -} - -void loadValueFromConfigFile(textFile_t * textFile, char *env, char *val, int len, char *butVal) -{ - strcpy(val, butVal); - - if (getValueInConfigFile(textFile, env, val, len) != 0) { - char line[STR_SIZE]; - - sprintf(line, "%s=\"%s\"", env, butVal); - list_add(textFile->text, strdup(line)); - - debug("Expanding config file [%s]", line); - } -} diff --git a/src/client/configFile.h b/src/client/configFile.h deleted file mode 100644 index fc56899..0000000 --- a/src/client/configFile.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef CONFIGFILE_H -#define CONFIGFILE_H - -#include "main.h" -#include "textFile.h" - -extern int isYesOrNO(char *s); -extern char *getYesOrNo(int n); - -extern int getValue(char *line, char *env, char *val, int len); -extern char *setValue(char *line, char *env, char *val); - -extern int getValueInConfigFile(textFile_t *textFile, char *env, char *val, int len); -extern int setValueInConfigFile(textFile_t *textFile, char *env, char *val); - -extern void loadValueFromConfigFile(textFile_t *textFile, char *env, char *val, - int len, char *butVal); - -#endif /* CONFIGFILE_H */ diff --git a/src/client/control.c b/src/client/control.c deleted file mode 100644 index bfc2428..0000000 --- a/src/client/control.c +++ /dev/null @@ -1,84 +0,0 @@ -#include <SDL.h> -#include <limits.h> -#include <assert.h> - -#include "main.h" -#include "control.h" - -control_t *control_new(SDLKey arg_up, SDLKey arg_right, SDLKey arg_left, - SDLKey arg_down, SDLKey arg_shot, SDLKey arg_switch) -{ - control_t *tmp; - - tmp = malloc(sizeof(control_t)); - memset(tmp, 0, sizeof(control_t)); - tmp->key[CONTROL_UP] = arg_up; - tmp->key[CONTROL_RIGHT] = arg_right; - tmp->key[CONTROL_LEFT] = arg_left; - tmp->key[CONTROL_DOWN] = arg_down; - tmp->key[CONTROL_SHOT] = arg_shot; - tmp->key[CONTROL_SWITCH] = arg_switch; - - return tmp; -} - -int control_get_key_route(control_t *my_control) -{ - Uint8 *map; - SDLKey ret_key; - int z; - int i; - - assert(my_control != NULL); - - ret_key = CONTROL_NONE; - - map = SDL_GetKeyState(NULL); - - for (i = 0; i < CONTROL_KEY_COUNT_ROUTE; i++) { - if (map[my_control->key[i]] == SDL_PRESSED) { - my_control->count[i]++; - } else { - my_control->count[i] = 0; - } - } - - z = INT_MAX; - - for (i = 0; i < CONTROL_KEY_COUNT_ROUTE; i++) { - if (my_control->count[i] > 0 && - my_control->count[i] < z && - map[my_control->key[i]] == SDL_PRESSED) { - z = my_control->count[i]; - ret_key = i; - } - } - - return ret_key; -} - -int control_get_key_action(control_t *my_control) -{ - Uint8 *map; - - assert(my_control != NULL); - - map = SDL_GetKeyState(NULL); - - if (map[my_control->key[CONTROL_SHOT]] == SDL_PRESSED) { - return CONTROL_SHOT; - } - - if (map[my_control->key[CONTROL_SWITCH]] == SDL_PRESSED) { - return CONTROL_SWITCH; - } - - return CONTROL_NONE; -} - -void control_destroy(control_t *my_control) -{ - assert(my_control != NULL); - - free(my_control); -} diff --git a/src/client/control.h b/src/client/control.h deleted file mode 100644 index 6b08189..0000000 --- a/src/client/control.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef CONTROL_H -#define CONTROL_H - -#define CONTROL_NONE -1 -#define CONTROL_UP 0 -#define CONTROL_RIGHT 1 -#define CONTROL_LEFT 2 -#define CONTROL_DOWN 3 -#define CONTROL_SHOT 4 -#define CONTROL_SWITCH 5 - -#define CONTROL_KEY_COUNT_ROUTE 4 -#define CONTROL_KEY_COUNT 6 - -typedef struct control_struct { - SDLKey key[CONTROL_KEY_COUNT]; - int count[CONTROL_KEY_COUNT]; -} control_t; - -extern control_t *control_new(SDLKey arg_up, SDLKey arg_right, - SDLKey arg_left, SDLKey arg_down, - SDLKey arg_shot, SDLKey arg_switch); -extern int control_get_key_route(control_t *my_control); -extern int control_get_key_action(control_t *my_control); -extern void control_destroy(control_t *my_control); - -#endif /* CONTROL_H */ diff --git a/src/client/font.c b/src/client/font.c deleted file mode 100644 index d052cbb..0000000 --- a/src/client/font.c +++ /dev/null @@ -1,156 +0,0 @@ -#include "main.h" -#include "interface.h" -#include "font.h" -#include "image.h" -#include "fontConfig.h" - -static TTF_Font *g_font; -static int fontSize; - -static bool_t isFontInit = FALSE; - -bool_t font_is_inicialized() -{ - return isFontInit; -} - -void font_init() -{ - char *arg[] = {":lang=he:outline=true:style=Book", "family", "style", "weight", "file", NULL}; - char *font_file; - font_config_t *font_list; - int res; - - assert(interface_is_inicialized() == TRUE); - - res = fontconfig_init(); - - if (res != 0) { - return; - } - - font_list = fontconfig_find(arg); -/* - int i; - for(i = 0 ; font_list[i].path != NULL ; i++ ) - { - printf("path[%d]=%s\nflag[%d]=%s\n\n", i, font_list[i].path, i, font_list[i].flag); - } -*/ - if (font_list == NULL) { - error("Unable to locate a font"); - return; - } - - font_file = strdup(font_list[0].path); - fontSize = FONT_SIZE; - - fontconfig_del_list(font_list); - fontconfig_quit(); - - if (TTF_Init() == -1) { - free(font_file); - error("SDL: %s", SDL_GetError()); - return; - } - - accessExistFile(font_file); - - g_font = TTF_OpenFont(font_file, fontSize); - TTF_SetFontStyle(g_font, TTF_STYLE_NORMAL); - - debug("Loading font [%s]", font_file); - - free(font_file); - isFontInit = TRUE; -} - -/* - * Shows text *string on coordinates [x,y] with RGB color - */ -void font_draw(char *string, int x, int y, int r, int g, int b) -{ - SDL_Surface *text; - image_t *image; - SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE}; - - assert( string != NULL ); - - text = TTF_RenderUTF8_Blended(g_font, string, font_color); - - /* because if string=="" TTF_RenderUTF8_Blended returns NULL */ - if (text != NULL) { - image = image_new(text); - image_draw(image, x, y, 0, 0, image->w, image->h); - image_destroy(image); - } -} - -void font_drawMaxSize(char *s, int x, int y, int w, int h, int r, int g, int b) -{ - SDL_Rect src_rect, dst_rect; - SDL_Surface *text; - image_t *i; - SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE}; - int my_w, my_h; - - assert(s != NULL); - - text = TTF_RenderUTF8_Blended(g_font, s, font_color); - - my_w = text->w; - - if (my_w > w) { - my_w = w; - } - - my_h = text->h; - - if (my_w > w) { - my_h = h; - } - - src_rect.x = 0; - src_rect.y = 0; - src_rect.w = my_w; - src_rect.h = my_h; - - dst_rect.x = x; - dst_rect.y = y; - - i = image_new(text); - - /* possibly broken */ - image_draw(i, x, y, 0, 0, my_w, my_h); - image_destroy(i); -} - -/* - * Returns size of the font - */ -int font_get_size() -{ - return fontSize; -} - -void font_text_size(char *s, int *w, int *h) -{ - assert(s != NULL); - assert(w != NULL); - assert(h != NULL); - - TTF_SizeUTF8(g_font, s, w, h); -} - -/* - * Frees font from memory - */ -void font_quit() -{ - TTF_CloseFont(g_font); - TTF_Quit(); - - debug("Unloading font"); - - isFontInit = FALSE; -} diff --git a/src/client/font.h b/src/client/font.h deleted file mode 100644 index 8ee2cf0..0000000 --- a/src/client/font.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef FONT_H -#define FONT_H - -#include <SDL_ttf.h> -#include <assert.h> -#include "main.h" - -#define COLOR_WHITE 255, 255, 255 -#define COLOR_BLACK 0, 0, 0 - -#define COLOR_RED 255, 0, 0 -#define COLOR_GREEN 0, 255, 0 -#define COLOR_BLUE 0, 0, 255 -#define COLOR_YELLOW 255, 255, 0 - -#define FONT_SIZE 16 - -extern bool_t font_is_inicialized(); -extern void font_init(); -extern void font_draw(char *s, int x, int y, int r, int g, int b); -extern void font_drawMaxSize(char *s, int x, int y, int w, int h, - int r, int g, int b); -extern int font_get_size(); -extern void font_text_size(char *s, int *w, int *h); -extern void font_quit(); - -#endif /* FONT_H */ diff --git a/src/client/fontConfig.c b/src/client/fontConfig.c deleted file mode 100644 index b0e0741..0000000 --- a/src/client/fontConfig.c +++ /dev/null @@ -1,102 +0,0 @@ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <fontconfig/fontconfig.h> - -#include "main.h" -#include "fontConfig.h" - -int fontconfig_init() -{ - int res; - - res = FcInit(); - - if (res == 0) { - error("Unable to init fontconfig library"); - return 1; - } - - return 0; -} - -font_config_t *fontconfig_find(char **arg) -{ - FcObjectSet *os; - FcFontSet *fs; - FcPattern *pat; - font_config_t *ret; - int i; - - os = NULL; - ret = NULL; - - for (i = 0; arg[i] != NULL; i++) { - if (i == 0) { - pat = FcNameParse((FcChar8 *) arg[i]); - } - - if (os == NULL) { - os = FcObjectSetCreate(); - } - - FcObjectSetAdd(os, arg[i]); - } - - if (os == NULL) { - os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, (char *) 0); - } - - fs = FcFontList(0, pat, os); - FcObjectSetDestroy(os); - - if (pat != NULL) { - FcPatternDestroy(pat); - } - - if (fs != NULL) { - int j; - - ret = malloc((fs->nfont + 1) * sizeof(font_config_t)); - - for (j = 0; j < fs->nfont; j++) { - FcChar8 *font; - FcChar8 *file; - - font = FcNameUnparse(fs->fonts[j]); - - if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) { - ret[j].path = strdup((char *) file); - } - - ret[j].flag = strdup((char *) font); - free(font); - } - - ret[fs->nfont].path = NULL; - ret[fs->nfont].flag = NULL; - - FcFontSetDestroy(fs); - } - - return ret; -} - -void fontconfig_del_list(font_config_t *list) -{ - int i; - - for (i = 0; list[i].path != NULL; i++) { - free(list[i].path); - free(list[i].flag); - } - - free(list); -} - -int fontconfig_quit() -{ - FcFini(); - return 0; -} diff --git a/src/client/fontConfig.h b/src/client/fontConfig.h deleted file mode 100644 index 09e75a3..0000000 --- a/src/client/fontConfig.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef FONT_CONFIG_H -#define FONT_CONFIG_H - -typedef struct font_config_struct { - char *path; - char *flag; -} font_config_t; - -extern int fontconfig_init(); -extern font_config_t *fontconfig_find(char **arg); -extern void fontconfig_del_list(font_config_t *list); -extern int fontconfig_quit(); - -#endif /* FONT_CONFIG_H */ diff --git a/src/client/game.c b/src/client/game.c deleted file mode 100644 index 2481cee..0000000 --- a/src/client/game.c +++ /dev/null @@ -1,127 +0,0 @@ -#include <time.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <assert.h> -#include <sys/stat.h> - -#include "main.h" -#include "tux.h" -#include "homeDirector.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "config.h" - -#include "image.h" -#include "layer.h" -#include "interface.h" -#include "screen.h" -#include "font.h" -#include "settingKeys.h" -#include "panel.h" -#include "radar.h" -#include "mouse_buffer.h" - -#ifndef NO_SOUND -#include "audio.h" -#include "sound.h" -#include "music.h" -#endif - -#include "world.h" -#include "mainMenu.h" -#include "analyze.h" -#include "setting.h" -#include "settingKeys.h" -#include "gameType.h" -#include "downArena.h" -#include "choiceArena.h" -#include "table.h" -#include "credits.h" -#include "browser.h" - -static void initGame() -{ - home_director_create(); - - mouse_buffer_init(); - interface_init(); - font_init(); - layer_init(); - image_init(); -#ifndef NO_SOUND - audio_init(); - sound_init(); - music_init(); -#endif /* NO_SOUND */ - screen_init(); - arena_file_init(); - tux_init(); - item_init(); - shot_init(); - panel_init(); - world_init(); - config_init(); - - main_menu_init(); - analyze_init(); - choice_arena_init(); - setting_init(); - setting_key_int(); - game_type_init(); - down_arena_init(); - scredits_init(); - table_init(); - browser_init(); -} - -void game_quit() -{ - mouse_buffer_quit(); - interface_quit(); - - main_menu_quit(); - analyze_quit(); - setting_quit(); - setting_key_quit(); - game_type_quit(); - down_arena_quit(); - choice_arena_quit(); - scredits_quit(); - table_quit(); - browser_quit(); - config_quit(); - - layer_quit(); - font_quit(); - screen_quit(); - arena_file_quit(); - item_quiy(); - tux_quit(); - shot_quit(); - panel_quit(); - world_quit(); - image_quit(); -#ifndef NO_SOUND - sound_quit(); - music_quit(); - audio_quit(); -#endif /* NO_SOUND */ - - debug("Shutting down the game"); - - exit(0); -} - -void game_start() -{ - initGame(); - - screen_start("mainMenu"); - - interface_event(); - game_quit(); -} diff --git a/src/client/game.h b/src/client/game.h deleted file mode 100644 index e5f2d86..0000000 --- a/src/client/game.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef GAME_H -#define GAME_H - -extern void game_quit(); -extern void game_start(); - -#endif /* GAME_H */ diff --git a/src/client/hotKey.c b/src/client/hotKey.c deleted file mode 100644 index 5471101..0000000 --- a/src/client/hotKey.c +++ /dev/null @@ -1,149 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "myTimer.h" - -#include "interface.h" -#include "hotKey.h" - -typedef struct hotKey_struct { - SDLKey key; - bool_t active; - void (*handler) (); -} hotKey_t; - -static list_t *listHotKey; -static my_time_t lastActive; - -static hotKey_t *newHotKey(SDLKey key, void (*handler) ()) -{ - hotKey_t *new; - - new = malloc(sizeof(hotKey_t)); - new->key = key; - new->active = TRUE; - new->handler = handler; - - return new; -} - -static void destroyHotKey(hotKey_t *hotkey) -{ - free(hotkey); -} - -static hotKey_t *findHotkey(SDLKey key) -{ - int i; - - for (i = 0; i < listHotKey->count; i++) { - hotKey_t *this; - - this = (hotKey_t *) listHotKey->list[i]; - - if (this->key == key) { - return this; - } - } - - return NULL; -} - -void hot_key_init() -{ - listHotKey = list_new(); - lastActive = timer_get_current_time(); -} - -void hot_key_register(SDLKey key, void (*handler) ()) -{ - hotKey_t *hotkey; -/* - if (findHotkey(key) != NULL) { - assert(!"Conflict with register key"); - } -*/ - hotkey = newHotKey(key, handler); - list_ins(listHotKey, 0, hotkey); -} - -void hot_key_unregister(SDLKey key) -{ - hotKey_t *hotkey; - int my_index; - - hotkey = findHotkey(key); - - if (hotkey == NULL) { - fatal("Unregistering not registered hotkey"); - } - - my_index = list_search(listHotKey, hotkey); - assert(my_index != -1); - list_del_item(listHotKey, my_index, destroyHotKey); -} - -void hot_key_enable(SDLKey key) -{ - hotKey_t *hotkey; - - hotkey = findHotkey(key); - - if (hotkey == NULL) { - fatal("Enabling not registered hotkey"); - } - - lastActive = timer_get_current_time(); - hotkey->active = TRUE; -} - -void hot_key_disable(SDLKey key) -{ - hotKey_t *hotkey; - - hotkey = findHotkey(key); - - if (hotkey == NULL) { - fatal("Disabling not registered hotkey"); - } - - lastActive = timer_get_current_time(); - hotkey->active = FALSE; -} - -void hot_key_event() -{ - my_time_t currentTime; - Uint8 *mapa; - int i; - - currentTime = timer_get_current_time(); - - if (currentTime - lastActive < HOTKEY_ACTIVE_INTERVAL) { - return; - } - - mapa = SDL_GetKeyState(NULL); - - for (i = 0; i < listHotKey->count; i++) { - hotKey_t *this; - - this = (hotKey_t *) listHotKey->list[i]; - - if (mapa[this->key] == SDL_PRESSED && this->active == TRUE) { - lastActive = timer_get_current_time(); - /*printf("hotKey = %d\n", this->key);*/ - this->handler(); - - return; - } - } -} - -void hot_key_quit() -{ - list_destroy_item(listHotKey, destroyHotKey); -} diff --git a/src/client/hotKey.h b/src/client/hotKey.h deleted file mode 100644 index 51b027f..0000000 --- a/src/client/hotKey.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef HOTKEY_H -#define HOTKEY_H - -#define HOTKEY_ACTIVE_INTERVAL 500 - -extern void hot_key_init(); -extern void hot_key_register(SDLKey key, void (*handler) ()); -extern void hot_key_unregister(SDLKey key); -extern void hot_key_enable(SDLKey key); -extern void hot_key_disable(SDLKey key); -extern void hot_key_event(); -extern void hot_key_quit(); - -#endif /* HOTKEY_H */ diff --git a/src/client/image.c b/src/client/image.c deleted file mode 100644 index cdd7c9b..0000000 --- a/src/client/image.c +++ /dev/null @@ -1,323 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "storage.h" - -#include "interface.h" -#include "image.h" - -static list_t *listStorage; - -static bool_t isImageDataInit = FALSE; - -bool_t image_is_inicialized() -{ - return isImageDataInit; -} - -/* - * Initialization of global list of images - */ -void image_init() -{ - assert(interface_is_inicialized() == TRUE); - - debug("Initializing image database"); - - listStorage = storage_new(); - isImageDataInit = TRUE; -} - -static SDL_Surface *loadImage(const char *filename, int alpha) -{ - SDL_Surface *tmp; - SDL_Surface *ret; - - char str[STR_PATH_SIZE]; - - if (isFillPath(filename)) { - strcpy(str, filename); - } else { - sprintf(str, PATH_IMAGE "%s", filename); - } - - accessExistFile(str); - - if ((tmp = IMG_Load(str)) == NULL) { - error("SDL: %s", SDL_GetError()); - return NULL; - } - - if ((ret = (alpha) ? SDL_DisplayFormatAlpha(tmp) : SDL_DisplayFormat(tmp)) == NULL) { - error("SDL: %s", SDL_GetError()); - SDL_FreeSurface(tmp); - return NULL; - } - - SDL_FreeSurface(tmp); - return ret; -} - - -image_t *image_new_sdl(SDL_Surface *surface) -{ - image_t *new; - - assert(surface != NULL); - - new = malloc(sizeof(image_t)); - new->image = surface; - new->w = surface->w; - new->h = surface->h; - - return new; -} - -#ifdef SUPPORT_OPENGL -/* - * returns closest bigger power of 2 to i - */ -unsigned int closestpoweroftwo(unsigned int i) -{ - int p; - p=0; - while (i) { - i = i >> 1; - p++; - } - return 1 << (p - 0); -} - -/* convert from SDL_Surface to image_t - * WARNING: image_new is indestructive to surface, caller is responsible for freeing surface - * surface is not needed for image_t after execution of image_new - */ - -image_t *image_new_opengl(SDL_Surface *surface) -{ - image_t *new; - Uint32 rmask, gmask, bmask, amask; - SDL_Surface *sdl_rgba_surface; - unsigned int bpp; - - /* it is unoptimal to blit to buffer surface before actual loading to opengl texture but it is safer and simpler to implement */ - /* it is unoptimal to always use RGBA texture */ - - /* we set properties of our buffer sdl_rgba_surface */ - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; - bpp = 32; /* bits per pixel */ - - assert(surface != NULL); - - new = malloc(sizeof(image_t)); - new->w = surface->w; - new->h = surface->h; - - /* because some older hw is really slow when using textures with width and height which is not a power of two */ - new->tw = closestpoweroftwo(new->w); - new->th = closestpoweroftwo(new->h); - - sdl_rgba_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, new->tw, new->th, bpp, rmask, gmask, bmask, amask); - - SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE); /* we unset SDL_SRCALPHA to preserve alpha channel of original image */ - SDL_BlitSurface(surface, 0, sdl_rgba_surface, 0); - - SDL_LockSurface(sdl_rgba_surface); /* we ensure that we can read pixels from sdl_rgba_surface */ - - GLuint tid; - glGenTextures(1, &tid); /* we ask for free texture id */ - glBindTexture(GL_TEXTURE_2D, tid); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* scale linearly when image bigger than texture */ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* scale linearly when image smalled than texture */ - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new->tw, new->th, 0, GL_RGBA, GL_UNSIGNED_BYTE, sdl_rgba_surface->pixels); - SDL_UnlockSurface(sdl_rgba_surface); - SDL_FreeSurface(sdl_rgba_surface); - - new->tex_id = tid; - - SDL_FreeSurface(surface); - - return new; -} -#endif /* SUPPORT_OPENGL */ - -image_t *image_new(SDL_Surface *surface) -{ -#ifdef SUPPORT_OPENGL - if (interface_is_use_open_gl()) { - return image_new_opengl(surface); - } else { - return image_new_sdl(surface); - } -#else /* SUPPORT_OPENGL */ - return image_new_sdl(surface); -#endif /* SUPPORT_OPENGL */ -} - -void image_destroy(image_t *p) -{ - assert(p != NULL); - -#ifndef SUPPORT_OPENGL - SDL_FreeSurface((SDL_Surface *) p->image); -#else /* SUPPORT_OPENGL */ - if (interface_is_use_open_gl()) { - glDeleteTextures(1, &p->tex_id); - } else { - SDL_FreeSurface((SDL_Surface *) p->image); - } -#endif /* SUPPORT_OPENGL */ - - free(p); -} - -/* - * Adds an image to the global image list - * *file - name of the image file - * *name - name of the image (is used for searching in the list) - * alpha - [0] without alpha channel; [1] with alpha channel - */ -image_t *image_add(char *file, int alpha, char *name, char *group) -{ - SDL_Surface *surface; - image_t *new; - - assert(file != NULL); - assert(name != NULL); - assert(group != NULL); - - surface = loadImage(file, alpha); - new = image_new(surface); - - storage_add(listStorage, group, name, new); - - debug("Loading image [%s]", file); - - return new; -} - -/* - * Returns pointer to image with name *name in the global image list - */ -image_t *image_get(char *group, char *name) -{ - assert(group != NULL); - assert(name != NULL); - - return storage_get(listStorage, group, name); -} - -void image_del(char *group, char *name) -{ - assert(group != NULL); - assert(name != NULL); - - storage_del(listStorage, group, name, image_destroy); -} - -void image_del_all_image_in_group(char *group) -{ - assert(group != NULL); - - storage_del_all(listStorage, group, image_destroy); -} - -void image_draw_sdl(image_t *p, int x, int y, int px, int py, int w, int h) -{ - static SDL_Surface *screen = NULL; - SDL_Rect dst_rect, src_rect; - - if (screen == NULL) { - screen = interface_get_screen(); - } - - dst_rect.x = x; - dst_rect.y = y; - - src_rect.x = px; - src_rect.y = py; - src_rect.w = w; - src_rect.h = h; - - SDL_BlitSurface(p->image, &src_rect, screen, &dst_rect); -} - -#ifdef SUPPORT_OPENGL -/* - * Draws image on screen at [x,y], with width w and height h, top-left corner on image is at [px,py] - */ -void image_draw_opengl(image_t *image, int x,int y, int px, int py, int w, int h) -{ - /* x - coordinate of left border; xx - coordinate of right border - * y - coordinate of top border; yy - coordinate of bottom border - * t - texture space; d - screen space - */ - float t_x, t_y, t_xx, t_yy; - float d_x, d_y, d_xx, d_yy; - - /* screen space */ - d_x = x; - d_y = y; - d_xx = x+w; - d_yy = y+h; - - /* texture space (remember that texture space is from 0.0 to 1.0) */ - t_x = px / (float) image->tw; - t_y = py / (float) image->th; - t_xx = (px+w) / (float) image->tw; - t_yy = (py+h) / (float) image->th; - - glBindTexture(GL_TEXTURE_2D, image->tex_id); - - /* - * 2--4 - * |\ | - * | \| - * 1--3 - */ - glBegin(GL_TRIANGLE_STRIP); - /* 1 */ - glTexCoord2f(t_x,t_yy); - glVertex2f(d_x, d_yy); - /* 2 */ - glTexCoord2f(t_x, t_y); - glVertex2f(d_x, d_y); - /* 3 */ - glTexCoord2f(t_xx, t_yy); - glVertex2f(d_xx, d_yy); - /* 4 */ - glTexCoord2f(t_xx,t_y); - glVertex2f(d_xx, d_y); - glEnd(); -} -#endif /* SUPPORT_OPENGL */ - -void image_draw(image_t *image, int x,int y, int px, int py, int w, int h) -{ -#ifdef SUPPORT_OPENGL - if (interface_is_use_open_gl()) { - image_draw_opengl(image, x, y, px, py, w, h); - } else { - image_draw_sdl(image, x, y, px, py, w, h); - } -#else /* SUPPORT_OPENGL */ - image_draw_sdl(image, x, y, px, py, w, h); -#endif /* SUPPORT_OPENGL */ -} - -/* - * Frees global image list - */ -void image_quit() -{ - debug("Shutting down image database"); - - storage_destroy(listStorage, image_destroy); - isImageDataInit = FALSE; -} diff --git a/src/client/image.h b/src/client/image.h deleted file mode 100644 index 9f225c6..0000000 --- a/src/client/image.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef IMAGE_H -#define IMAGE_H - -#include "main.h" -#include "interface.h" - -#define IMAGE_NO_ALPHA 0 -#define IMAGE_ALPHA 1 - -#define IMAGE_GROUP_BASE "base" -#define IMAGE_GROUP_EXTENDSIO "extension" -#define IMAGE_GROUP_USER "user" -#define IMAGE_GROUP_OTHER "other" - -#ifdef SUPPORT_OPENGL -#include <SDL_opengl.h> -#endif - -/* -typedef struct image_struct { - char *name; - char *group; - SDL_Surface *image; -} image_data_t; -*/ - -#ifndef SUPPORT_OPENGL -typedef struct image_struct { - int w; - int h; - SDL_Surface *image; -} image_t; -#else /* SUPPORT_OPENGL */ -typedef struct image_struct { - int w; /* width of actual picture */ - int h; /* height of actual picture */ - SDL_Surface *image; - GLuint tw; /* width of allocated texture */ - GLuint th; /* height of allocated texture */ - GLuint tex_id; /* texture id */ -} image_t; -#endif /* SUPPORT_OPENGL */ - -extern bool_t image_is_inicialized(); -extern void image_init(); -extern image_t *image_new(SDL_Surface *surface); -extern void image_destroy(image_t * p); -extern image_t *image_add(char *file, int alpha, char *name, char *group); -extern image_t *image_get(char *group, char *name); -extern void image_del(char *group, char *name); -extern void image_del_all_image_in_group(char *group); -extern void image_draw(image_t * p, int x, int y, int px, int py, int w, int h); -extern void image_quit(); - -#endif /* IMAGE_H */ diff --git a/src/client/interface.c b/src/client/interface.c deleted file mode 100644 index 62d7f96..0000000 --- a/src/client/interface.c +++ /dev/null @@ -1,358 +0,0 @@ -#include <stdlib.h> - -#include "main.h" -#include "myTimer.h" - -#include "interface.h" -#include "screen.h" -#include "keyboardBuffer.h" -#include "hotKey.h" -#include "mouse_buffer.h" - -static SDL_Surface *screen; /* window surface */ -/*static SDL_Surface *my_surface;*/ -static SDL_TimerID timer; /* timer */ -static Uint32 g_win_flags; /* window flags */ - -static bool_t isInterfaceInit = FALSE; -static bool_t keyboardBufferEnabled = FALSE; /* flag that controls organising keys into the buffer */ -static bool_t use_open_gl; - -bool_t interface_is_use_open_gl() -{ - return use_open_gl; -} - -bool_t interface_is_inicialized() -{ - return isInterfaceInit; -} - -static Uint32 TimerCallback(Uint32 interval, void *param) -{ - SDL_Event event; - - event.type = SDL_USEREVENT; - event.user.code = USR_EVT_TIMER; - event.user.data1 = NULL; - event.user.data2 = NULL; - - SDL_PushEvent(&event); - - return interval; -} - -void interface_enable_keyboard_buffer() -{ - keyboardBufferEnabled = TRUE; -} - -void interface_disable_keyboard_buffer() -{ - keyboardBufferEnabled = FALSE; -} - -void hotkey_screen() -{ - if (g_win_flags & SDL_FULLSCREEN) { - g_win_flags &= ~SDL_FULLSCREEN; - } else { - g_win_flags |= SDL_FULLSCREEN; - } - - if (SDL_WM_ToggleFullScreen(screen) == 0) { - error("Unable to switch to the fullscreen mode"); - - SDL_FreeSurface(screen); - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, WIN_BPP, g_win_flags); - - if (screen == NULL) { - error("Unable to switch to the window mode: %s", SDL_GetError()); - return; - } - } -} - -#ifdef SUPPORT_OPENGL -/** - * Sets video mode and sets up OpenGL for this change - */ -SDL_Surface *SetVideoMode(int width, int height, int bpp, Uint32 flags) -{ - SDL_Surface *rval = 0; - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - rval = SDL_SetVideoMode(width, height, bpp, flags); - - glClearColor(0.0f, 0.0f, 0.0f, 0.5f); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - glClearDepth(1.0); - glDisable(GL_DEPTH_TEST); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -#ifdef SDL_GL_SWAP_CONTROL - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); -#endif /* SDL_GL_SWAP_CONTROL */ - - glViewport(0, 0, width, height); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glEnable(GL_TEXTURE_2D); - - return rval; -} -#endif /* SUPPORT_OPENGL */ - -int interface_init() -{ - debug("Initializing SDL"); - - /* initialization of SDL */ - if (SDL_Init(SDL_SUBSYSTEMS) == -1) { - fatal("Unable to initialize SDL: %s", SDL_GetError()); - SDL_Quit(); - exit(0); - return -1; - } - -#ifndef SUPPORT_OPENGL - use_open_gl = FALSE; - g_win_flags = SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT; - - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); -#else /* SUPPORT_OPENGL */ - use_open_gl = !isParamFlag("--disable-opengl"); - - if (interface_is_use_open_gl()) { - g_win_flags = SDL_OPENGL; - screen = SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); - - if (screen == NULL) { - use_open_gl = FALSE; - g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT; - - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); - } - } else { - g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT; - screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags); - } -#endif /* SUPPORT_OPENGL */ - - if (screen == NULL) { - error("Unable to create interface: %s", SDL_GetError()); - SDL_Quit(); - return -1; - } - /** - * enable unicode by default for keyboard support - it is a bit more - * consuming than the nonsdl but can draw more characters - */ - SDL_EnableUNICODE(1); - SDL_WM_SetCaption(WINDOW_TITLE, NULL); - - /* keyboard repeating */ - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL); - - keyboard_buffer_init(KEYBOARD_BUFFER_SIZE); - - hot_key_init(); - hot_key_register(SDLK_F1, hotkey_screen); - - srand((unsigned) time(NULL)); - isInterfaceInit = TRUE; - - return 0; -} - -SDL_Surface *interface_get_screen() -{ - /*return my_surface;*/ - return screen; -} - -void interface_refresh() -{ -#ifndef SUPPORT_OPENGL - SDL_Flip(screen); -#else /* SUPPORT_OPENGL */ - if (interface_is_use_open_gl()) { - SDL_GL_SwapBuffers(); - } else { - SDL_Flip(screen); - } -#endif /* SUPPORT_OPENGL */ -} - -void interface_get_mouse_position(int *x, int *y) -{ - SDL_GetMouseState(x, y); -} - -int interface_is_mouse_clicket() -{ - return SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT); -} - -int interface_is_press_any_key() -{ - Uint8 *mapa; - int i; - - mapa = SDL_GetKeyState(NULL); - - for (i = SDLK_BACKSPACE; i < SDLK_KP9; i++) { - if (mapa[i] == SDL_PRESSED) { - return 1; - } - } - - return 0; -} - -void printPressAnyKey() -{ - Uint8 *mapa; - int i; - - mapa = SDL_GetKeyState(NULL); - - for (i = SDLK_BACKSPACE; i < SDLK_KP9; i++) { - if (mapa[i] == SDL_PRESSED) { - debug("Pressed key [SDL: %d]", i); - } - } - -} - -int hack_slow() -{ - static time_t lastTime = 0; - static bool_t isSlowHack = FALSE; - time_t currentTime; - - currentTime = timer_get_current_time(); - - if (lastTime == 0) { - lastTime = currentTime; - return 0; - } - - /*printf("DEBUG: time interval %d\n", currentTime - lastTime);*/ - - if (isSlowHack == FALSE && currentTime - lastTime >= 100) { - /*printf("start slow hack (%d)\n", currentTime - lastTime);*/ - isSlowHack = TRUE; - lastTime = timer_get_current_time(); - return 1; - } - - if (isSlowHack == TRUE && currentTime - lastTime >= 50) { - /*printf("stop slow hack (%d)\n", currentTime - lastTime);*/ - isSlowHack = FALSE; - lastTime = timer_get_current_time(); - return 0; - } - - if (isSlowHack == TRUE) { - /*printf("hack time interval %d\n", currentTime - lastTime);*/ - } - - lastTime = timer_get_current_time(); - - return isSlowHack; -} - -int eventAction() -{ - SDL_Event event; - - while (SDL_WaitEvent(&event)) { - switch (event.type) { - case SDL_MOUSEBUTTONDOWN: - mouse_buffer_event(&event.button); - break; - - case SDL_KEYDOWN: - switch (event.key.keysym.sym) { - default: - /* it is not necessary to always use the buffer */ - if (keyboardBufferEnabled == TRUE) { - keyboard_buffer_push(event.key.keysym); - } - break; - } - break; - - case SDL_USEREVENT: - switch (event.user.code) { - case USR_EVT_TIMER: - if (hack_slow()) { - break; - } - - screen_draw(); - screen_event(); - hot_key_event(); - screen_switch(); - mouse_buffer_clean(); - break; - - default: - break; - } - break; - - /* change the window size */ - case SDL_VIDEORESIZE: - screen = SDL_SetVideoMode(event.resize.w, event.resize.h, - WIN_BPP, g_win_flags); - - if (screen == NULL) { - error("Unable to change resolution of the window: %s", SDL_GetError()); - return 0; - } - break; - - /* termination request */ - case SDL_QUIT: - return -1; - break; - - default: - break; - } - } - - return 0; -} - -void interface_event() -{ - while (1) { - if (eventAction() == -1) { - return; - } - } -} - -void interface_quit() -{ - debug("Shutting down SDL"); - - hot_key_quit(); - keyboard_buffer_quit(); - - SDL_Quit(); - - isInterfaceInit = FALSE; -} diff --git a/src/client/interface.h b/src/client/interface.h deleted file mode 100644 index 2fd23e9..0000000 --- a/src/client/interface.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef INTERFACE_H -#define INTERFACE_H - -#include <stdio.h> -#include <string.h> -#include <time.h> - -#include <SDL.h> -#include <SDL_image.h> -#include <SDL_thread.h> - -#include "main.h" - -#ifdef SUPPORT_OPENGL -#include <SDL_opengl.h> -#endif - -/* -#include <SDL_net.h> -*/ - -#define SDL_SUBSYSTEMS SDL_INIT_VIDEO|SDL_INIT_TIMER - -/* the window title */ -#define WINDOW_TITLE "Tuxánci " TUXANCI_VERSION -#define WIN_BPP 0 -#define USR_EVT_TIMER 0 - -/* timing of the timer [in ms] */ -#define INTERVAL 50 - -/* size of the keyboard buffer (in characters) */ -#define KEYBOARD_BUFFER_SIZE 256 - -extern bool_t interface_is_inicialized(); -extern bool_t interface_is_use_open_gl(); -extern void interface_enable_keyboard_buffer(); -extern void interface_disable_keyboard_buffer(); -extern int interface_init(); -extern SDL_Surface *interface_get_screen(); -extern void interface_get_mouse_position(int *x, int *y); -extern int interface_is_mouse_clicket(); -extern int interface_is_press_any_key(); -extern void interface_refresh(); -extern void interface_event(); -extern void interface_quit(); - -#endif /* INTERFACE_H */ diff --git a/src/client/keyboardBuffer.c b/src/client/keyboardBuffer.c deleted file mode 100644 index 129389f..0000000 --- a/src/client/keyboardBuffer.c +++ /dev/null @@ -1,142 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <SDL.h> - -#include "keyboardBuffer.h" - -static keyboardBuffer_t *keyboardBuffer = NULL; - -static SDL_keysym emptyKey; - -/* - * Key buffer initialization - * the parameter sets the number of keys - */ -void keyboard_buffer_init(int size) -{ - assert(size > 0); - assert(keyboardBuffer == NULL); - - keyboardBuffer = malloc(sizeof(keyboardBuffer_t)); - assert(keyboardBuffer != NULL); - memset(keyboardBuffer, 0, sizeof(keyboardBuffer_t)); - - keyboardBuffer->buff = malloc(size * sizeof(SDL_keysym)); - assert(keyboardBuffer->buff != NULL); - memset(keyboardBuffer->buff, 0, size * sizeof(SDL_keysym)); - - keyboardBuffer->begin = 0; - keyboardBuffer->end = 0; - keyboardBuffer->count = 0; - keyboardBuffer->size = size; - - emptyKey.sym = SDLK_UNKNOWN; -} - -/* - * Empty the buffer - */ -void keyboard_buffer_clear() -{ - assert(keyboardBuffer != NULL); - - keyboardBuffer->begin = 0; - keyboardBuffer->end = 0; - keyboardBuffer->count = 0; - memset(keyboardBuffer->buff, 0, keyboardBuffer->size * sizeof(SDL_keysym)); -} - -/* - * Adds key to the end of the buffer. If the buffer is overrun, - * an error is printed to stderr and the key is dropped. - */ -bool_t keyboard_buffer_push(SDL_keysym key) -{ - assert(keyboardBuffer != NULL); - - if (keyboardBuffer->count >= keyboardBuffer->size) { - error("Keyboard buffer overrun - dropping the key [%02x]", key.sym); - return FALSE; - } - - keyboardBuffer->buff[keyboardBuffer->end] = key; - - keyboardBuffer->end++; - if (keyboardBuffer->end >= keyboardBuffer->size) { - keyboardBuffer->end = 0; - } - - keyboardBuffer->count++; - - return TRUE; -} - -/* - * Takes out first key from the buffer and returns it. If the buffer is empty, - * an error is printed to stderr and SDLK_UNKNOWN is returned. - */ -SDL_keysym keyboard_buffer_pop() -{ - assert(keyboardBuffer != NULL); - - if (keyboardBuffer->count <= 0) { - error("Keyboard buffer underrun"); - return emptyKey; - } - - SDL_keysym key = keyboardBuffer->buff[keyboardBuffer->begin]; - keyboardBuffer->buff[keyboardBuffer->begin] = emptyKey; - - keyboardBuffer->begin++; - - if (keyboardBuffer->begin >= keyboardBuffer->size) { - keyboardBuffer->begin = 0; - } - - keyboardBuffer->count--; - - return key; -} - -/* - * The buffer size - */ -int keyboard_buffer_get_size() -{ - assert(keyboardBuffer != NULL); - return keyboardBuffer->size; -} - -/* - * Number of keys in the buffer - */ -int keyboard_buffer_get_count() -{ - assert(keyboardBuffer != NULL); - return keyboardBuffer->count; -} - - -bool_t keyboard_buffer_is_any_key() -{ - return keyboard_buffer_get_count() > 0 ? TRUE : FALSE; -} - -/* - * Frees the buffer. If it is not empty, information - * about filling is prined to stderr first. - */ -void keyboard_buffer_quit() -{ - assert(keyboardBuffer != NULL); - - if (keyboardBuffer->count > 0) { - warning("Shutting down non-empty keyboard buffer"); - } - - free(keyboardBuffer->buff); - free(keyboardBuffer); - keyboardBuffer = NULL; -} diff --git a/src/client/keyboardBuffer.h b/src/client/keyboardBuffer.h deleted file mode 100644 index e3596ef..0000000 --- a/src/client/keyboardBuffer.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef KEYBOARD_BUFFER_H -#define KEYBOARD_BUFFER_H - -#include <SDL.h> -#include "main.h" - -typedef struct { - SDL_keysym *buff; /* buffer of keys (code according to the SDLKey enum) */ - int size; /* up-to-date size of allocated buffer (size == number of saveable keys) */ - int count; /* up-to-date number of keys saved in the buffer */ - int begin; /* pointer to the beginning of the buffer (i.e. where to add new keys to) */ - int end; /* pointer to the end of the buffer (i.e. where to remove old keys from) */ -} keyboardBuffer_t; - -extern void keyboard_buffer_init(int size); -extern void keyboard_buffer_clear(); -extern bool_t keyboard_buffer_push(SDL_keysym key); -extern SDL_keysym keyboard_buffer_pop(); -extern int keyboard_buffer_get_size(); -extern int keyboard_buffer_get_count(); -extern bool_t keyboard_buffer_is_any_key(); -extern void keyboard_buffer_quit(); - -#endif /* KEYBOARD_BUFFER_H */ diff --git a/src/client/layer.c b/src/client/layer.c deleted file mode 100644 index 22c037d..0000000 --- a/src/client/layer.c +++ /dev/null @@ -1,200 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" - -#include "interface.h" -#include "layer.h" -#include "image.h" - -#include "world.h" - -static list_t *listLayer; - -static bool_t isLayerInit = FALSE; - -bool_t layer_is_inicialized() -{ - return isLayerInit; -} - -/* - * Initialize global list of the layers - */ -void layer_init() -{ - listLayer = list_new(); - isLayerInit = TRUE; -} - -/* - * Add image to queue for rendering - * *img - image to be rendered - * x,y - coordinates where to draw the image - * w,h - width and height of rendered part of image - * px,py - coordinates of upper left corner of rendered part of image - * layer - on which layer to draw - * [0] - under Tuxanek; [1] same as Tuxanek; [2] over Tuxanek - */ -void addLayer(image_t *img, int x, int y, int px, int py, int w, int h, int player) -{ - layer_t *new; - layer_t *actual; - int actual_value; - int new_value; - int i; - - assert(img != NULL); - - new = malloc(sizeof(layer_t)); - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->px = px; - new->py = py; - new->layer = player; - new->image = img; - - new_value = player * WINDOW_SIZE_Y + y + h; - - /* here we are sure, that we want to insert it somewhere else than the begining */ - for (i = 0; i < listLayer->count; i++) { - actual = list_get(listLayer, i); - - actual_value = actual->layer * WINDOW_SIZE_Y + actual->y + actual->h; - - if (actual_value > new_value) { - list_ins(listLayer, i, new); - return; - } - } - - list_add(listLayer, new); -} - -/* - * Draws all items onto the screen according to the layer and coordinate Y+H - * After drawing frees the list - */ -void layer_draw_all() -{ - layer_t *this; - int i; - - for (i = 0; i < listLayer->count; i++) { - this = (layer_t *) listLayer->list[i]; - - image_draw(this->image, this->x, this->y, this->px, this->py, this->w, this->h); - } - - /*printf("listLayer->count = %d\n", listLayer->count);*/ - - list_destroy_item(listLayer, free); - listLayer = list_new(); -} - -void layer_draw_center(int x, int y) -{ - layer_t *this; - int screen_x, screen_y; - int i; - /*int count = 0;*/ - - arena_get_center_screen(&screen_x, &screen_y, x, y, WINDOW_SIZE_X, WINDOW_SIZE_Y); - - for (i = 0; i < listLayer->count; i++) { - this = (layer_t *) listLayer->list[i]; - - if (this->x + this->w < screen_x || - this->x > screen_x + WINDOW_SIZE_X || - this->y + this->h < screen_y || - this->y > screen_y + WINDOW_SIZE_Y) { - continue; - } - /*count++;*/ - - image_draw(this->image, this->x - screen_x, this->y - screen_y, - this->px, this->py, this->w, this->h); - - /*printf("%d %d\n", this->x - screen_x, this->y - screen_y);*/ - } - - /*printf("count = %d\n", count);*/ - - list_destroy_item(listLayer, free); - listLayer = list_new(); -} - -void layer_draw_slpit(int local_x, int local_y, int x, int y, int w, int h) -{ - layer_t *this; - int i; - /*int count = 0;*/ - - for (i = 0; i < listLayer->count; i++) { - this = (layer_t *) listLayer->list[i]; - - if (this->x + this->w < x || this->x > x + w || - this->y + this->h < y || this->y > y + h) { - continue; - } - - /*count++;*/ - - if (local_x + (this->x - x) < local_x) { - int z; - - z = local_x - (local_x + (this->x - x)); - - this->x += z; - this->px += z; - this->w -= z; - } - - if (local_x + (this->x - x) + this->w > local_x + w) { - this->w -= (local_x + (this->x - x) + this->w) - (local_x + w); - } - - if (local_y + (this->y - y) < local_y) { - int z; - - z = local_y - (local_y + (this->y - y)); - - this->y += z; - this->py += z; - this->h -= z; - } - - if (local_y + (this->y - y) + this->h > local_y + h) { - this->h -= (local_y + (this->y - y) + this->h) - (local_y + h); - } - - image_draw(this->image, local_x + (this->x - x), local_y + (this->y - y), - this->px, this->py, this->w, this->h); - - /*printf("%d %d\n", this->x - screen_x, this->y - screen_y);*/ - } - - /*printf("count = %d\n", count);*/ - - list_destroy_item(listLayer, free); - listLayer = list_new(); -} - -void layer_flush() -{ - list_destroy_item(listLayer, free); - listLayer = list_new(); -} - -/* - * Delete list of layers from memory - */ -void layer_quit() -{ - list_destroy_item(listLayer, free); - isLayerInit = FALSE; -} diff --git a/src/client/layer.h b/src/client/layer.h deleted file mode 100644 index 3642a0a..0000000 --- a/src/client/layer.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef MY_LAYER_H -#define MY_LAYER_H - -#include "main.h" -#include "image.h" - -typedef struct layer_str { - /* coordinates of the image */ - int x; - int y; - - /* size of the image */ - int w; - int h; - - /* coordinates of a part of the surface */ - int px; - int py; - - /* size of a part of the surface */ - int pw; - int ph; - - /* in which layer to lay it */ - int layer; - - /* image of the surface */ - image_t *image; -} layer_t; - -extern bool_t layer_is_inicialized(); -extern void layer_init(); - -extern void addLayer(image_t *img, int x, int y, int px, int py, int w, int h, int player); - -extern void layer_draw_all(); -extern void layer_draw_center(int x, int y); -extern void layer_draw_slpit(int local_x, int local_y, int x, int y, int w, int h); -extern void layer_flush(); -extern void layer_quit(); - -#endif /* MY_LAYER_H */ diff --git a/src/client/mouse_buffer.c b/src/client/mouse_buffer.c deleted file mode 100644 index f301b28..0000000 --- a/src/client/mouse_buffer.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <SDL.h> - -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "mouse_buffer.h" - -static list_t *list_event; - -typedef struct mouse_event_struct { - Uint8 button; - int x; - int y; -} mouse_event_t; - -static mouse_event_t *mouse_event_new(int x, int y, Uint8 button) -{ - mouse_event_t *mouse_event; - - mouse_event = malloc(sizeof(mouse_event_t)); - mouse_event->x = x; - mouse_event->y = y; - mouse_event->button = button; - - return mouse_event; -} - -static void mouse_event_destroy(mouse_event_t *mouse_event) -{ - assert(mouse_event != NULL); - free(mouse_event); -} - -int mouse_buffer_init() -{ - list_event = list_new(); - - return 0; -} - -int mouse_buffer_event(SDL_MouseButtonEvent *button) -{ - assert(button != NULL); - assert(list_event != NULL); - - switch (button->button) { - case SDL_BUTTON_LEFT: - case SDL_BUTTON_RIGHT: - /*printf("mouse button - pos(%d,%d)\n", button->x, button->y);*/ - - list_add(list_event, mouse_event_new(button->x, button->y, button->button)); - - return 1; - break; - - default: - break; - } - - return 0; -} - -int mouse_buffer_clean() -{ - assert(list_event != NULL); - - list_destroy_item(list_event, mouse_event_destroy); - list_event = list_new(); - - return 0; -} - -bool_t mouse_buffer_is_on_area(int x, int y, int w, int h, unsigned int flag) -{ - mouse_event_t *mouse_event; - int i; - - assert(list_event != NULL); - - if (flag & MOUSE_BUF_MOTION) { - int mouse_x, mouse_y; - - SDL_GetMouseState(&mouse_x, &mouse_y); - - if (mouse_x >= x && mouse_y >= y && - mouse_x < x+w && mouse_y < y+h) { - return TRUE; - } - } else { - for (i = 0; i < list_event->count; i++) { - mouse_event = (mouse_event_t *) list_event->list[i]; - - if ((mouse_event->x >= x && mouse_event->y >= y && - mouse_event->x < x + w && mouse_event->y < y + h) || - (flag & MOUSE_BUF_AREA_NONE)) { - if (flag & MOUSE_BUF_CLICK_LEFT) { - if (mouse_event->button == SDL_BUTTON_LEFT) { - return TRUE; - } else { - return FALSE; - } - } - - if (flag & MOUSE_BUF_CLICK_RIGHT) { - if (mouse_event->button == SDL_BUTTON_RIGHT) { - return TRUE; - } else { - return FALSE; - } - } - - if (flag & MOUSE_BUF_CLICK) { - if (mouse_event->button == SDL_BUTTON_LEFT || - mouse_event->button == SDL_BUTTON_RIGHT) { - return TRUE; - } else { - return FALSE; - } - } - - return TRUE; - } - } - } - - return FALSE; -} - -int mouse_buffer_quit() -{ - assert(list_event != NULL); - list_destroy_item(list_event, mouse_event_destroy); - - return 0; -} diff --git a/src/client/mouse_buffer.h b/src/client/mouse_buffer.h deleted file mode 100644 index 7cc6588..0000000 --- a/src/client/mouse_buffer.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MOUSE_EVENT_H -#define MOUSE_EVENT_H - -#include <SDL.h> - -#define MOUSE_BUF_MOTION 1 -#define MOUSE_BUF_CLICK_LEFT 2 -#define MOUSE_BUF_CLICK_RIGHT 4 -#define MOUSE_BUF_CLICK 8 -#define MOUSE_BUF_AREA_NONE 16 - -extern int mouse_buffer_init(); -extern int mouse_buffer_event(SDL_MouseButtonEvent *button); -extern int mouse_buffer_clean(); -extern bool_t mouse_buffer_is_on_area(int x, int y, int w, int h, unsigned int flag); -extern int mouse_buffer_quit(); - -#endif /* MOUSE_EVENT_H */ diff --git a/src/client/panel.c b/src/client/panel.c deleted file mode 100644 index 43dab1a..0000000 --- a/src/client/panel.c +++ /dev/null @@ -1,159 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" - -#include "interface.h" -#include "image.h" -#include "panel.h" -#include "font.h" -#include "chat.h" - -static image_t *g_panel; -static image_t *g_shot; -static image_t *g_icon[ITEM_COUNT]; -static image_t *g_bonus; - -static bool_t isPanelInit = FALSE; - -bool_t panel_is_inicialized() -{ - return isPanelInit; -} - -void panel_init() -{ - assert(image_is_inicialized() == TRUE); - assert(interface_is_inicialized() == TRUE); - - g_panel = image_add("panel.png", IMAGE_ALPHA, "panel", IMAGE_GROUP_BASE); - g_shot = image_add("panel_shot.png", IMAGE_ALPHA, "panel_image_shot",IMAGE_GROUP_BASE); - g_bonus = image_add("graf.bonus.png", IMAGE_ALPHA, "panel_graf_bonus", IMAGE_GROUP_BASE); - g_icon[GUN_SIMPLE] = image_add("icon.gun.simple.png", IMAGE_ALPHA, "panel_gun", IMAGE_GROUP_BASE); - g_icon[GUN_DUAL_SIMPLE] = image_add("icon.gun.dual.simple.png", IMAGE_ALPHA, "panel_dual", IMAGE_GROUP_BASE); - g_icon[GUN_TOMMY] = image_add("icon.gun.tommy.png", IMAGE_ALPHA, "panel_tommy", IMAGE_GROUP_BASE); - g_icon[GUN_SCATTER] = image_add("icon.gun.scatter.png", IMAGE_ALPHA, "panel_scatter", IMAGE_GROUP_BASE); - g_icon[GUN_LASSER] = image_add("icon.gun.lasser.png", IMAGE_ALPHA, "panel_lasser", IMAGE_GROUP_BASE); - g_icon[GUN_MINE] = image_add("icon.gun.mine.png", IMAGE_ALPHA, "panel_mine", IMAGE_GROUP_BASE); - g_icon[GUN_BOMBBALL] = image_add("icon.gun.bombball.png", IMAGE_ALPHA, "panel_bombball", IMAGE_GROUP_BASE); - g_icon[BONUS_SPEED] = image_add("icon.bonus.speed.png", IMAGE_ALPHA, "panel_speed", IMAGE_GROUP_BASE); - g_icon[BONUS_SHOT] = image_add("icon.bonus.shot.png", IMAGE_ALPHA, "panel_shot", IMAGE_GROUP_BASE); - g_icon[BONUS_TELEPORT] = image_add("icon.bonus.teleport.png", IMAGE_ALPHA, "panel_teleport", IMAGE_GROUP_BASE); - g_icon[BONUS_GHOST] = image_add("icon.bonus.ghost.png", IMAGE_ALPHA, "panel_ghost", IMAGE_GROUP_BASE); - g_icon[BONUS_4X] = image_add("icon.bonus.4x.png", IMAGE_ALPHA, "panel_4x", IMAGE_GROUP_BASE); - g_icon[BONUS_HIDDEN] = image_add("icon.bonus.hidden.png", IMAGE_ALPHA, "panel_hidden", IMAGE_GROUP_BASE); - - isPanelInit = TRUE; -} - -static void drawScore(tux_t *tux_left, tux_t *tux_right) -{ - char strScoreLine[STR_SIZE]; - int w, h; - - if (tux_right != NULL && tux_left != NULL) { - sprintf(strScoreLine, "%d : %d", tux_right->score, tux_left->score); - } else if (tux_right != NULL) { - sprintf(strScoreLine, "%d", tux_right->score); - } else if (tux_left != NULL) { - sprintf(strScoreLine, "%d", tux_left->score); - } - - font_text_size(strScoreLine, &w, &h); - font_draw(strScoreLine, PANEL_SCORE_LOCATION_X, PANEL_SCORE_LOCATION_Y, COLOR_WHITE); -} - -static void shot_draw(int n, int x, int y) -{ - image_draw(g_shot, x, y, n * PANEL_SHOT_WIDTH, 0, PANEL_SHOT_WIDTH, PANEL_SHOT_HEIGHT); -} - -static void shot_drawInfo(tux_t *tux, int x, int y) -{ - int i; - - if (tux->pickup_time > 0) { - for (i = 0; i < tux->pickup_time / 3; i++) { - shot_draw(PANEL_SHOT_LINE, x + i * PANEL_SHOT_WIDTH, y); - } - - return; - } - - for (i = 0; i < GUN_MAX_SHOT; i++) { - if (tux->shot[tux->gun] > i) { - shot_draw(PANEL_SHOT_FILL, x + i * (PANEL_SHOT_WIDTH + 2), y); - } else { - shot_draw(PANEL_SHOT_EMPTY, x + i * (PANEL_SHOT_WIDTH + 2), y); - } - } -} - -static void drawGunInfo(tux_t *tux, int x, int y) -{ - image_draw(g_icon[tux->gun], x, y, 0, 0, g_icon[tux->gun]->w, g_icon[tux->gun]->h); -} - -static void drawBonusInfo(tux_t *tux, int x, int y) -{ - image_draw(g_icon[tux->bonus], x, y, 0, 0, g_icon[tux->bonus]->w, g_icon[tux->bonus]->h); -} - -static void drawGrafBonus(tux_t *tux, int x, int y) -{ - int offset; - - offset = tux->bonus_time / 5; - image_draw(g_bonus, x, y, 0, 0, g_bonus->w, g_bonus->h / 2); - image_draw(g_bonus, x + 1, y, 0, g_bonus->h / 2, offset, g_bonus->h / 2); -} - -static void tux_drawRight(tux_t *tux) -{ - if (tux == NULL) { - return; - } - - shot_drawInfo(tux, PANEL_LOCATION_X + 740, PANEL_LOCATION_Y + 34); - drawGunInfo(tux, PANEL_LOCATION_X + 620, PANEL_LOCATION_Y + 30); - - if (tux->bonus != BONUS_NONE) { - drawGrafBonus(tux, PANEL_LOCATION_X + 460, PANEL_LOCATION_Y + 50); - drawBonusInfo(tux, PANEL_LOCATION_X + 460, PANEL_LOCATION_Y + 30); - } -} - -static void tux_drawLeft(tux_t *tux) -{ - if (tux == NULL) { - return; - } - - shot_drawInfo(tux, PANEL_LOCATION_X + 8, PANEL_LOCATION_Y + 34); - drawGunInfo(tux, PANEL_LOCATION_X + 130, PANEL_LOCATION_Y + 30); - - if (tux->bonus != BONUS_NONE) { - drawGrafBonus(tux, PANEL_LOCATION_X + 200, PANEL_LOCATION_Y + 50); - drawBonusInfo(tux, PANEL_LOCATION_X + 200, PANEL_LOCATION_Y + 30); - } -} - -void panel_draw(tux_t *tux_right, tux_t *tux_left) -{ - image_draw(g_panel, PANEL_LOCATION_X, PANEL_LOCATION_Y, 0, 0, g_panel->w, g_panel->h); - drawScore(tux_right, tux_left); - - if (chat_is_recived_new_msg()) { - font_draw(_("Recived new chat message"), PANEL_LOCATION_X, PANEL_LOCATION_Y, COLOR_WHITE); - } - - tux_drawRight(tux_right); - tux_drawLeft(tux_left); -} - -void panel_quit() -{ - isPanelInit = FALSE; -} diff --git a/src/client/panel.h b/src/client/panel.h deleted file mode 100644 index 8ae92b3..0000000 --- a/src/client/panel.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef PANEL_H -#define PANEL_H - -#include "main.h" -#include "list.h" - -#define PANEL_SCORE_LOCATION_X (WINDOW_SIZE_X / 2 - w / 2) -#define PANEL_SCORE_LOCATION_Y (WINDOW_SIZE_Y - 40) - -#define PANEL_SHOT_WIDTH 4 -#define PANEL_SHOT_HEIGHT 8 - -#define PANEL_LOCATION_X 0 -#define PANEL_LOCATION_Y (WINDOW_SIZE_Y - g_panel->h) - -#define PANEL_SHOT_FILL 0 -#define PANEL_SHOT_EMPTY 1 -#define PANEL_SHOT_LINE 2 - -extern bool_t panel_is_inicialized(); -extern void panel_init(); -extern void panel_draw(tux_t *tux_right, tux_t *tux_left); -extern void panel_quit(); - -#endif /* PANEL_H */ diff --git a/src/client/pauza.c b/src/client/pauza.c deleted file mode 100644 index 61f80ea..0000000 --- a/src/client/pauza.c +++ /dev/null @@ -1,61 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "myTimer.h" - -#include "interface.h" -#include "image.h" -#include "pauza.h" -#include "hotKey.h" - -static image_t *g_pauza; -static bool_t activePauza; - -static void switchPauzed() -{ - if (activePauza == TRUE) { - activePauza = FALSE; - } else { - activePauza = TRUE; - } -} - -static void hotkey_pauze() -{ - switchPauzed(); -} - -void pauza_init() -{ - g_pauza = image_add("pauza.png", IMAGE_ALPHA, "pauza", IMAGE_GROUP_USER); - activePauza = FALSE; - - hot_key_register(SDLK_p, hotkey_pauze); -} - -void pauza_draw() -{ - if (activePauza) { - image_draw(g_pauza, - WINDOW_SIZE_X / 2 - g_pauza->w / 2, - WINDOW_SIZE_Y / 2 - g_pauza->h / 2, - 0, 0, g_pauza->w, g_pauza->h); - } -} - -void pauza_event() -{ -} - -bool_t pauza_is_active() -{ - return activePauza; -} - -void pauza_quit() -{ - hot_key_unregister(SDLK_p); -} diff --git a/src/client/pauza.h b/src/client/pauza.h deleted file mode 100644 index a432d5f..0000000 --- a/src/client/pauza.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef PAUZA_H -#define PAUZA_H - -#include "main.h" - -#define PAUZE_ACTIVE_TIME_INTERVAL 1000 - -extern void pauza_init(); -extern void pauza_draw(); -extern void pauza_event(); -extern bool_t pauza_is_active(); -extern void pauza_quit(); - -#endif /* PAUZA_H */ diff --git a/src/client/radar.c b/src/client/radar.c deleted file mode 100644 index e8457e9..0000000 --- a/src/client/radar.c +++ /dev/null @@ -1,113 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "arena.h" - -#include "interface.h" -#include "image.h" -#include "radar.h" - -static image_t *g_radar; -static list_t *listRadar; - -typedef struct radar_item_struct { - int id; - int x, y; - int type; -} radar_item_t; - -static radar_item_t *newRadarItem(int id, int x, int y, int type) -{ - radar_item_t *new; - - new = malloc(sizeof(radar_item_t)); - new->id = id; - new->x = x; - new->y = y; - new->type = type; - - return new; -} - -static void destroyRadarItem(radar_item_t *p) -{ - assert(p != NULL); - free(p); -} - -void radar_add(int id, int x, int y, int type) -{ - int i; - - for (i = 0; i < listRadar->count; i++) { - radar_item_t *thisRadarItem; - - thisRadarItem = (radar_item_t *) listRadar->list[i]; - - if (thisRadarItem->id == id) { - thisRadarItem->x = x; - thisRadarItem->y = y; - thisRadarItem->type = type; - return; - } - } - - list_add(listRadar, newRadarItem(id, x, y, type)); -} - -void radar_del(int id) -{ - int i; - - for (i = 0; i < listRadar->count; i++) { - radar_item_t *thisRadarItem; - - thisRadarItem = (radar_item_t *) listRadar->list[i]; - - if (thisRadarItem->id == id) { - list_del_item(listRadar, i, destroyRadarItem); - return; - } - } -} - -void radar_init() -{ - assert(image_is_inicialized() == TRUE); - assert(interface_is_inicialized() == TRUE); - - g_radar = image_add("radar.png", IMAGE_NO_ALPHA, "radar", IMAGE_GROUP_USER); - listRadar = list_new(); -} - -void radar_draw(arena_t *arena) -{ - int i; - - image_draw(g_radar, RADAR_LOCATION_X, RADAR_LOCATION_Y, 0, 0, RADAR_SIZE_X + 2, RADAR_SIZE_Y + 2); - - for (i = 0; i < listRadar->count; i++) { - radar_item_t *thisRadarItem; - int x, y; - - thisRadarItem = (radar_item_t *) listRadar->list[i]; - - x = (((float) RADAR_SIZE_X) / ((float) arena->w)) * ((float) thisRadarItem->x); - - y = (((float) RADAR_SIZE_Y) / ((float) arena->h)) * ((float) thisRadarItem->y); - - if (x >= 0 && x <= RADAR_SIZE_X && y >= 0 && y <= RADAR_SIZE_Y) { - image_draw(g_radar, RADAR_LOCATION_X + 1 + x, RADAR_LOCATION_Y + 1 + y, - 2 * thisRadarItem->type, RADAR_SIZE_Y + 2, 2, 2); - } - } -} - -void radar_quit() -{ - list_destroy_item(listRadar, destroyRadarItem); -} diff --git a/src/client/radar.h b/src/client/radar.h deleted file mode 100644 index 8bc2a01..0000000 --- a/src/client/radar.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef RADAR_H -#define RADAR_H - -#include "arena.h" - -#define RADAR_LOCATION_X (WINDOW_SIZE_X - 110) -#define RADAR_LOCATION_Y 10 - -#define RADAR_SIZE_X 100 -#define RADAR_SIZE_Y 100 - -#define RADAR_TYPE_YOU 3 -#define RADAR_TYPE_TUX 1 -#define RADAR_TYPE_ITEM 2 - -extern void radar_init(); -extern void radar_add(int id, int x, int y, int type); -extern void radar_del(int id); -extern void radar_draw(arena_t *arena); -extern void radar_quit(); - -#endif /* RADAR_H */ diff --git a/src/client/saveDialog.c b/src/client/saveDialog.c deleted file mode 100644 index 7a0c316..0000000 --- a/src/client/saveDialog.c +++ /dev/null @@ -1,134 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "space.h" -#include "arena.h" -#include "net_multiplayer.h" - -#include "interface.h" -#include "saveDialog.h" -#include "image.h" -#include "font.h" -#include "saveLoad.h" -#include "hotKey.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_textfield.h" - -static image_t *g_background; -static bool_t activeSaveDialog; - -static widget_t *widgetLabelMsg; -static widget_t *widgetTextFieldName; - -static widget_t *widgetButtonSave; -static widget_t *widgetButtonBack; - -static void switchTerm() -{ - if (activeSaveDialog == TRUE) { - activeSaveDialog = FALSE; - hot_key_unregister(SDLK_ESCAPE); - } else { - activeSaveDialog = TRUE; - hot_key_register(SDLK_ESCAPE, switchTerm); - } -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == widgetButtonSave) { - save_arena(text_field_get_text(widgetTextFieldName), arena_get_current()); - switchTerm(); - } - - if (button == widgetButtonBack) { - switchTerm(); - } -} - -static void hotkey_saveDialog() -{ - switchTerm(); -} - -void save_dialog_init() -{ - activeSaveDialog = FALSE; - - g_background = image_get(IMAGE_GROUP_BASE, "screen_main"); - - widgetLabelMsg = label_new(_("Save as:"), SAVE_DIALOG_LOCATIN_X + 20, - SAVE_DIALOG_LOCATIN_Y + 20, - WIDGET_LABEL_LEFT); - - widgetTextFieldName = text_field_new("", WIDGET_TEXTFIELD_FILTER_ALPHANUM, - widgetLabelMsg->x + widgetLabelMsg->w + 10, - widgetLabelMsg->y); - - widgetButtonSave = button_new(_("Save"), SAVE_DIALOG_LOCATIN_X + 20, - SAVE_DIALOG_LOCATIN_Y + 60, - eventWidget); - - widgetButtonBack = button_new(_("Back"), SAVE_DIALOG_LOCATIN_X + 20 + - WIDGET_BUTTON_WIDTH + 20, - SAVE_DIALOG_LOCATIN_Y + 60, - eventWidget); - - hot_key_register(SDLK_F2, hotkey_saveDialog); -} - -bool_t save_dialog_is_active() -{ - return activeSaveDialog; -} - -void save_dialog_draw() -{ - if (activeSaveDialog == FALSE) { - return; - } - - image_draw(g_background, SAVE_DIALOG_LOCATIN_X, - SAVE_DIALOG_LOCATIN_Y, - SAVE_DIALOG_LOCATIN_X, - SAVE_DIALOG_LOCATIN_Y, - SAVE_DIALOG_SIZE_X, - SAVE_DIALOG_SIZE_Y); - - label_draw(widgetLabelMsg); - text_field_draw(widgetTextFieldName); - button_draw(widgetButtonSave); - button_draw(widgetButtonBack); -} - -void save_dialog_event() -{ - if (activeSaveDialog == FALSE) { - return; - } - - text_field_event(widgetTextFieldName); - button_event(widgetButtonSave); - button_event(widgetButtonBack); - -} - -void save_dialog_quit() -{ - hot_key_unregister(SDLK_F2); - - label_destroy(widgetLabelMsg); - text_field_destroy(widgetTextFieldName); - button_destroy(widgetButtonSave); - button_destroy(widgetButtonBack); -} diff --git a/src/client/saveDialog.h b/src/client/saveDialog.h deleted file mode 100644 index f5fb91f..0000000 --- a/src/client/saveDialog.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef SAVE_DIALOG_H -#define SAVE_DIALOG_H - -#define SAVEDIALOG_ACTIVE_TIME_INTERVAL 500 - -#define SAVE_DIALOG_SIZE_X 320 -#define SAVE_DIALOG_SIZE_Y 120 - -#define SAVE_DIALOG_LOCATIN_X (WINDOW_SIZE_X / 2 - SAVE_DIALOG_SIZE_X / 2) -#define SAVE_DIALOG_LOCATIN_Y (WINDOW_SIZE_Y / 2 - SAVE_DIALOG_SIZE_Y / 2) - -extern void save_dialog_init(); -extern bool_t save_dialog_is_active(); -extern void save_dialog_draw(); -extern void save_dialog_event(); -extern void save_dialog_quit(); - -#endif /* SAVE_DIALOG_H */ diff --git a/src/client/saveLoad.c b/src/client/saveLoad.c deleted file mode 100644 index 899ef25..0000000 --- a/src/client/saveLoad.c +++ /dev/null @@ -1,258 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "textFile.h" -#include "homeDirector.h" - -#include "arena.h" -#include "arenaFile.h" -#include "tux.h" -#include "shot.h" -#include "item.h" -#include "modules.h" - -#include "saveLoad.h" - -#include "choiceArena.h" -#include "world.h" - -static void action_saveTux(space_t *space, tux_t *tux, textFile_t *textFile) -{ - char str[STR_PROTO_SIZE]; - - snprintf(str, STR_PROTO_SIZE, - "TUX %d %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", - tux->id, tux->x, tux->y, tux->status, tux->position, - tux->control, tux->frame, tux->score, tux->name, tux->gun, - tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], - tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], - tux->shot[GUN_LASSER], tux->shot[GUN_MINE], - tux->shot[GUN_BOMBBALL], tux->bonus_time, tux->pickup_time); - - list_add(textFile->text, strdup(str)); -} - -static void action_saveShot(space_t *space, shot_t *shot, textFile_t *textFile) -{ - char str[STR_PROTO_SIZE]; - - snprintf(str, STR_PROTO_SIZE, "SHOT %d %d %d %d %d %d %d %d %d", - shot->id, shot->x, shot->y, shot->px, shot->py, - shot->position, shot->gun, shot->author_id, - shot->isCanKillAuthor); - - list_add(textFile->text, strdup(str)); -} - -static void action_saveItem(space_t *space, item_t *item, textFile_t *textFile) -{ - char str[STR_PROTO_SIZE]; - - snprintf(str, STR_PROTO_SIZE, "ITEM %d %d %d %d %d %d %d", - item->id, item->type, item->x, item->y, - item->count, item->frame, item->author_id); - - list_add(textFile->text, strdup(str)); -} - -static void saveContextArenaToTextFile(textFile_t *textFile, arena_t *arena) -{ - char str[STR_PROTO_SIZE]; - - sprintf(str, "ARENA %s %d %d", - arena_file_get_net_name(choice_arena_get()), - arena->countRound, arena->max_countRound); - - list_add(textFile->text, strdup(str)); - - space_action(arena->spaceTux, action_saveTux, textFile); - space_action(arena->spaceShot, action_saveShot, textFile); - space_action(arena->spaceItem, action_saveItem, textFile); -} - -void save_arena(char *filename, arena_t *arena) -{ - textFile_t *textFile; - char path[STR_PATH_SIZE]; - - sprintf(path, "%s%s%s.sav", home_director_get(), PATH_SEPARATOR, filename); - - debug("Saving game [%s]", path); - - textFile = text_file_new(path); - - if (textFile != NULL) { - saveContextArenaToTextFile(textFile, arena); - text_file_save(textFile); - text_file_destroy(textFile); - } else { - error("Unable to save game [%s]", path); - } -} - -static arena_t *load_arenaFromLine(char *line) -{ - char cmd[STR_SIZE]; - char name[STR_SIZE]; - int countRound; - int max_countRound; - arena_t *arena; - - sscanf(line, "%s %s %d %d", cmd, name, &countRound, &max_countRound); - - world_set_arena(arena_file_get_file_format_net_name(name)); - - arena = arena_get_current(); - arena->max_countRound = max_countRound; - arena->countRound = countRound; - - return arena; -} - -static void loadTuxFromLine(char *line, arena_t *arena) -{ - char cmd[STR_PROTO_SIZE]; - char name[STR_NAME_SIZE]; - int id, x, y, status, position, frame, score, control; - int myGun, myBonus; - int gun1, gun2, gun3, gun4, gun5, gun6, gun7; - int time1, time2; - tux_t *tux; - - sscanf(line, - "%s %d %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", - cmd, &id, &x, &y, &status, &position, &control, &frame, &score, - name, &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, - &gun7, &time1, &time2); - - tux = tux_new(); - tux_replace_id(tux, id); - space_add(arena->spaceTux, tux); - - tux->control = control; - - if (tux->control == TUX_CONTROL_KEYBOARD_RIGHT) { - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_RIGHT); - } - - if (tux->control == TUX_CONTROL_KEYBOARD_LEFT) { - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_LEFT); - } - - if (tux->control == TUX_CONTROL_AI) { - module_load("libmodAI"); - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_LEFT); - } - - space_move_object(arena->spaceTux, tux, x, y); - tux->status = status; - tux->position = position; - tux->frame = frame; - tux->score = score; - strcpy(tux->name, name); - tux->gun = myGun; - tux->bonus = myBonus; - tux->shot[GUN_SIMPLE] = gun1; - tux->shot[GUN_DUAL_SIMPLE] = gun2; - tux->shot[GUN_SCATTER] = gun3; - tux->shot[GUN_TOMMY] = gun4; - tux->shot[GUN_LASSER] = gun5; - tux->shot[GUN_MINE] = gun6; - tux->shot[GUN_BOMBBALL] = gun7; - tux->bonus_time = time1; - tux->pickup_time = time2; -} - -static void loadShotFromLine(char *line, arena_t *arena) -{ - char cmd[STR_PROTO_SIZE]; - int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor; - shot_t *shot; - - sscanf(line, "%s %d %d %d %d %d %d %d %d %d", - cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id, - &isCanKillAuthor); - - shot = shot_new(x, y, px, py, gun, author_id); - - shot_replace_id(shot, shot_id); - shot->isCanKillAuthor = isCanKillAuthor; - shot->position = position; - - if (shot->gun == GUN_LASSER) { - shot_transform_lasser(shot); - } - - space_add(arena->spaceShot, shot); -} - -static void loadItemFromLine(char *line, arena_t *arena) -{ - char cmd[STR_PROTO_SIZE]; - int id, type, x, y, count, frame, author_id, check_id; - item_t *item; - - sscanf(line, "%s %d %d %d %d %d %d %d %d", - cmd, &id, &type, &x, &y, &count, &frame, &author_id, &check_id); - - item = item_new(x, y, type, author_id); - - item_replace_id(item, id); - item->count = count; - item->frame = frame; - - space_add(arena->spaceItem, item); -} - -static arena_t *loadContextArenaFromTextFile(textFile_t *textFile) -{ - arena_t *arena; - int i; - - arena = NULL; - - for (i = 0; i < textFile->text->count; i++) { - char *line; - - line = (char *) textFile->text->list[i]; - - if (strncmp(line, "ARENA", 5) == 0) { - arena = load_arenaFromLine(line); - } - - if (strncmp(line, "TUX", 3) == 0) { - loadTuxFromLine(line, arena); - } - - if (strncmp(line, "SHOT", 4) == 0) { - loadShotFromLine(line, arena); - } - - if (strncmp(line, "ITEM", 4) == 0) { - loadItemFromLine(line, arena); - } - } - - return arena; -} - -void load_arena(char *filename) -{ - textFile_t *textFile; - char path[STR_PATH_SIZE]; - - sprintf(path, "%s%s%s", home_director_get(), PATH_SEPARATOR, filename); - - debug("Loading arena [%s]", path); - - textFile = text_file_load(path); - - if (textFile != NULL) { - loadContextArenaFromTextFile(textFile); - text_file_destroy(textFile); - } else { - error("Unable to load saved game [%s]", path); - } -} diff --git a/src/client/saveLoad.h b/src/client/saveLoad.h deleted file mode 100644 index eaf27e2..0000000 --- a/src/client/saveLoad.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef SAVELOAD_H -#define SAVELOAD_H - -#include "textFile.h" -#include "arena.h" - -extern void save_arena(char *filename, arena_t *arena); -extern void load_arena(char *filename); - -#endif /* SAVELOAD_H */ diff --git a/src/client/screen.c b/src/client/screen.c deleted file mode 100644 index 476cf3e..0000000 --- a/src/client/screen.c +++ /dev/null @@ -1,175 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" - -#include "interface.h" -#include "screen.h" -#include "layer.h" -#include "myTimer.h" - -/*#define DEBUG_TIME_DRAW*/ - -static screen_t *currentScreen; -static screen_t *futureScreen; - -static list_t *listScreen; - -static bool_t isScreenInit = FALSE; - -bool_t screen_is_inicialized() -{ - return isScreenInit; -} - -screen_t *screen_new(char *name, void (*fce_start) (), void (*fce_event) (), - void (*fce_draw) (), void (*fce_stop) ()) -{ - screen_t *new; - - assert(name != NULL); - assert(fce_start != NULL); - assert(fce_event != NULL); - assert(fce_draw != NULL); - assert(fce_stop != NULL); - - new = malloc(sizeof(screen_t)); - new->name = strdup(name); - new->fce_start = fce_start; - new->fce_event = fce_event; - new->fce_draw = fce_draw; - new->fce_stop = fce_stop; - - return new; -} - -void screen_destroy(screen_t *p) -{ - assert(p != NULL); - - free(p->name); - free(p); -} - -void screen_register(screen_t *p) -{ - assert(p != NULL); - - debug("Registering screen [%s]", p->name); - - list_add(listScreen, p); -} - -void screen_init() -{ - listScreen = list_new(); - - currentScreen = NULL; - futureScreen = NULL; - - isScreenInit = TRUE; -} - -static screen_t *findScreen(char *name) -{ - screen_t *this; - int i; - - for (i = 0; i < listScreen->count; i++) { - this = (screen_t *) listScreen->list[i]; - assert(this != NULL); - - if (strcmp(name, this->name) == 0) { - return this; - } - } - - return NULL; -} - -void screen_set(char *name) -{ - futureScreen = findScreen(name); - assert(futureScreen != NULL); -} - -void screen_switch() -{ - if (futureScreen == NULL) { - return; - } - - layer_flush(); - - if (currentScreen != NULL) { - debug("Stopping screen [%s]", currentScreen->name); - currentScreen->fce_stop(); - } - - currentScreen = futureScreen; - futureScreen = NULL; - - debug("Starting screen [%s]", currentScreen->name); - - currentScreen->fce_start(); -} - -void screen_start(char *name) -{ - screen_set(name); - screen_switch(); -} - -char *screen_get() -{ - assert(currentScreen != NULL); - return currentScreen->name; -} - -void screen_draw() -{ - static int count = 0; - - count++; - - if (count == 1) { - count = 0; - - assert(currentScreen != NULL); - -#ifdef DEBUG_TIME_DRAW - my_time_t prev; - - prev = timer_get_current_timeMicro(); -#endif /* DEBUG_TIME_DRAW */ - - currentScreen->fce_draw(); - - interface_refresh(); - -#ifdef DEBUG_TIME_DRAW - printf("c draw time = %d\n", timer_get_current_timeMicro() - prev); -#endif /* DEBUG_TIME_DRAW */ - } -} - - -void screen_event() -{ - assert(currentScreen != NULL); - - currentScreen->fce_event(); - -#ifdef DEBUG_TIME_EVENT - printf("c event time = %d\n", timer_get_current_timeMicro() - prev); -#endif /* DEBUG_TIME_EVENT */ -} - -void screen_quit() -{ - assert(listScreen != NULL); - - list_destroy_item(listScreen, screen_destroy); - isScreenInit = FALSE; -} diff --git a/src/client/screen.h b/src/client/screen.h deleted file mode 100644 index 718fb57..0000000 --- a/src/client/screen.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef SCREEN_H -#define SCREEN_H - -#include "main.h" - -typedef struct screen_struct { - char *name; - void (*fce_start) (); - void (*fce_event) (); - void (*fce_draw) (); - void (*fce_stop) (); -} screen_t; - -extern bool_t screen_is_inicialized(); - -extern screen_t *screen_new(char *name, void (*fce_start) (), - void (*fce_event) (), - void (*fce_draw) (), - void (*fce_stop) ()); - -extern void screen_destroy(screen_t *p); -extern void screen_register(screen_t *p); -extern void screen_init(); -extern void screen_set(char *name); -extern void screen_switch(); -extern void screen_start(char *name); -extern char *screen_get(); -extern void screen_draw(); -extern void screen_event(); -extern void screen_quit(); - -#endif /* SCREEN_H */ diff --git a/src/client/term.c b/src/client/term.c deleted file mode 100644 index dc4a51b..0000000 --- a/src/client/term.c +++ /dev/null @@ -1,157 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "myTimer.h" -#include "tux.h" -#include "space.h" -#include "arena.h" - -#include "interface.h" -#include "term.h" -#include "font.h" - -static list_t *listText; -static bool_t activeTerm; -static my_time_t lastActive; -static my_time_t lastRefresh; - -void term_init() -{ - listText = list_new(); - - activeTerm = FALSE; - lastActive = timer_get_current_time(); - lastRefresh = lastActive; -} - -static char *getStrGun(int gun) -{ - switch (gun) { - case GUN_SIMPLE: - return "revolver"; - case GUN_DUAL_SIMPLE: - return "dual revolver"; - case GUN_SCATTER: - return "scatter"; - case GUN_TOMMY: - return "tommy"; - case GUN_LASSER: - return "lasser"; - case GUN_MINE: - return "mine"; - case GUN_BOMBBALL: - return "bombball"; - default: - return "none"; - } - - return "gun_unknown"; -} - -static char *getStrBonus(int bonus) -{ - switch (bonus) { - case BONUS_SPEED: - return "speed"; - case BONUS_SHOT: - return "shot"; - case BONUS_TELEPORT: - return "teleport"; - case BONUS_GHOST: - return "ghost"; - case BONUS_4X: - return "4"; - case BONUS_HIDDEN: - return "hidden"; - default: - return "none"; - } - - return "bonus_unknown"; -} - -static void action_refreshTerm(space_t *space, tux_t *tux, void *p) -{ - char str[STR_SIZE]; - - sprintf(str, - "name: %s " - "score: %d " - "gun: %s " - "shot: %d " - "bonus: %s", - tux->name, tux->score, - getStrGun(tux->gun), tux->shot[tux->gun], getStrBonus(tux->bonus) - ); - - list_add(listText, strdup(str)); -} - -static void refreshTerm() -{ - arena_t *arena; - - arena = arena_get_current(); - - list_destroy_item(listText, free); - listText = list_new(); - - space_action(arena->spaceTux, action_refreshTerm, NULL); - - /*printf("refresh term..\n");*/ -} - -void term_draw() -{ - int i; - - if (activeTerm == FALSE) { - return; - } - - for (i = 0; i < listText->count; i++) { - char *line; - - line = (char *) listText->list[i]; - font_draw(line, 10, 10 + i * 20, COLOR_WHITE); - } -} - -static void switchTerm() -{ - if (activeTerm == TRUE) { - activeTerm = FALSE; - } else { - activeTerm = TRUE; - } -} - -void term_event() -{ - my_time_t currentTime; - Uint8 *mapa; - - mapa = SDL_GetKeyState(NULL); - - currentTime = timer_get_current_time(); - - if (currentTime - lastRefresh > TERM_REFRESH_TIME_INTERVAL) { - lastRefresh = currentTime; - refreshTerm(); - } - - if (mapa[SDLK_TAB] == SDL_PRESSED) { - if (currentTime - lastActive > TERM_ACTIVE_TIME_INTERVAL) { - lastActive = currentTime; - switchTerm(); - } - } -} - -void term_quit() -{ - assert(listText != NULL); - list_destroy_item(listText, free); -} diff --git a/src/client/term.h b/src/client/term.h deleted file mode 100644 index 719df2d..0000000 --- a/src/client/term.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef TERM_H -#define TERM_H - -#include "main.h" - -#define TERM_ACTIVE_TIME_INTERVAL 500 -#define TERM_REFRESH_TIME_INTERVAL 1000 - -extern void term_init(); -extern void term_draw(); -extern void term_event(); -extern void term_quit(); - -#endif /* TERM_H */ diff --git a/src/client/yes_no_dialog.c b/src/client/yes_no_dialog.c deleted file mode 100644 index d982933..0000000 --- a/src/client/yes_no_dialog.c +++ /dev/null @@ -1,134 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" - -#include "interface.h" -#include "hotKey.h" -#include "yes_no_dialog.h" -#include "image.h" -#include "font.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" - -static image_t *g_background; - -static widget_t *widgetLabelMsg; - -static widget_t *widgetButtonYes; -static widget_t *widgetButtonNo; - -static void (*handle_yes)(void *param); -static void (*handle_no)(void *param); -static void *param; -static bool_t active_dialog; - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - yes_no_dialog_set_active(FALSE); - - if (button == widgetButtonYes) { - handle_yes(param); - } - - if (button == widgetButtonNo) { - handle_no(param); - } -} - -void yes_no_dialog_init() -{ - active_dialog = FALSE; - - g_background = image_get(IMAGE_GROUP_BASE, "screen_main"); - - widgetLabelMsg = label_new("empty label", WINDOW_SIZE_X / 2, - YES_NO_DIALOG_LOCATIN_Y + 20, - WIDGET_LABEL_CENTER); - - widgetButtonYes = button_new(_("Yes"), YES_NO_DIALOG_LOCATIN_X + 20, - YES_NO_DIALOG_LOCATIN_Y + 60, - eventWidget); - - widgetButtonNo = button_new(_("No"), YES_NO_DIALOG_LOCATIN_X + 20 + - WIDGET_BUTTON_WIDTH + 20, - YES_NO_DIALOG_LOCATIN_Y + 60, - eventWidget); -} - -static void hotkey_escape() -{ - yes_no_dialog_set_active(FALSE); - handle_no(param); -} - -void yes_no_dialog_set_active(bool_t active) -{ - active_dialog = active; - - if (active_dialog == TRUE) { - hot_key_register(SDLK_ESCAPE, hotkey_escape); - } else { - hot_key_unregister(SDLK_ESCAPE); - } -} - -bool_t yes_no_dialog_is_active() -{ - return active_dialog; -} - -void yes_no_dialog_set(char *label, void *function_yes, void *function_no, void *p_param) -{ - label_destroy(widgetLabelMsg); - - widgetLabelMsg = label_new(label, WINDOW_SIZE_X / 2, - YES_NO_DIALOG_LOCATIN_Y + 20, - WIDGET_LABEL_CENTER); - - handle_yes = function_yes; - handle_no = function_no; - param = p_param; -} - -void yes_no_dialog_draw() -{ - if (active_dialog == FALSE) { - return; - } - - image_draw(g_background, YES_NO_DIALOG_LOCATIN_X, - YES_NO_DIALOG_LOCATIN_Y, - YES_NO_DIALOG_LOCATIN_X, - YES_NO_DIALOG_LOCATIN_Y, - YES_NO_DIALOG_SIZE_X, - YES_NO_DIALOG_SIZE_Y); - - label_draw(widgetLabelMsg); - button_draw(widgetButtonYes); - button_draw(widgetButtonNo); -} - -void yes_no_dialog_event() -{ - if (active_dialog == FALSE) { - return; - } - - button_event(widgetButtonYes); - button_event(widgetButtonNo); -} - -void yes_no_dialog_quit() -{ - label_destroy(widgetLabelMsg); - button_destroy(widgetButtonYes); - button_destroy(widgetButtonNo); -} diff --git a/src/client/yes_no_dialog.h b/src/client/yes_no_dialog.h deleted file mode 100644 index 3cef74c..0000000 --- a/src/client/yes_no_dialog.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef YES_NO_DIALOG_H -#define YES_NO_DIALOG_H - -#define YES_NO_ACTIVE_TIME_INTERVAL 500 - -#define YES_NO_DIALOG_SIZE_X 320 -#define YES_NO_DIALOG_SIZE_Y 120 - -#define YES_NO_DIALOG_LOCATIN_X (WINDOW_SIZE_X / 2 - YES_NO_DIALOG_SIZE_X / 2) -#define YES_NO_DIALOG_LOCATIN_Y (WINDOW_SIZE_Y / 2 - YES_NO_DIALOG_SIZE_Y / 2) - -extern void yes_no_dialog_init(); -extern void yes_no_dialog_set_active(bool_t active); -extern bool_t yes_no_dialog_is_active(); -extern void yes_no_dialog_set(char *label, void *function_yes, void *function_no, void *p_param); -extern void yes_no_dialog_draw(); -extern void yes_no_dialog_event(); -extern void yes_no_dialog_quit(); - -#endif /* YES_NO_DIALOG_H */ diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000..de05866 --- /dev/null +++ b/src/color.c @@ -0,0 +1,28 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "color.h" + +ta_color ta_color_white; +ta_color ta_color_black; + +void ta_color_init(void) { + ta_color_white = (ta_color){ 255, 255, 255, 255 }; + ta_color_black = (ta_color){ 0, 0, 0, 255 }; +}
\ No newline at end of file diff --git a/src/events.c b/src/events.c new file mode 100644 index 0000000..f107733 --- /dev/null +++ b/src/events.c @@ -0,0 +1,34 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ta_sdl.h" +#include "main.h" +#include "exit_key.h" +#include "keyboard.h" + +void ta_events_update(void) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT) { + ta_main_exit(1); + } else if (event.type == SDL_EVENT_KEY_DOWN) { + ta_keyboard_dispatch(event.key.key); + } + } +}
\ No newline at end of file diff --git a/src/exit_key.c b/src/exit_key.c new file mode 100644 index 0000000..263934b --- /dev/null +++ b/src/exit_key.c @@ -0,0 +1,42 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdbool.h> +#include "ta_sdl.h" +#include "main.h" +#include "logging.h" +#include "keyboard.h" + +#define TA_EXIT_KEY SDLK_ESCAPE + +bool ta_exit_key_ignore = false; + +void ta_exit_key_callback(void) { + if (ta_exit_key_ignore) { + ta_log("Escape key pressed but ta_exit_key_ignore is true, ignoring"); + return; + } else { + ta_log("Escape key pressed, exiting"); + ta_main_exit(0); + } +} + +void ta_exit_key_init(void) { + ta_keyboard_set_callback(TA_EXIT_KEY, ta_exit_key_callback); +}
\ No newline at end of file diff --git a/src/forever.c b/src/forever.c new file mode 100644 index 0000000..a8e0351 --- /dev/null +++ b/src/forever.c @@ -0,0 +1,37 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "forever.h" + +#ifdef __EMSCRIPTEN__ +#include <emscripten/emscripten.h> +#include <stdbool.h> +#endif // __EMSCRIPTEN__ + +void ta_forever(ta_loop loop) { +#ifndef __EMSCRIPTEN__ + while (1) { + loop(); + } +#else // __EMSCRIPTEN__ + int fps = 0; + bool simulate_infinite_loop = true; + emscripten_set_main_loop(loop, fps, simulate_infinite_loop); +#endif // __EMSCRIPTEN__ +}
\ No newline at end of file diff --git a/src/gpu.c b/src/gpu.c new file mode 100644 index 0000000..9d4bd9d --- /dev/null +++ b/src/gpu.c @@ -0,0 +1,209 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdbool.h> +#include "ta_sdl.h" +#include "window.h" +#include "files.h" +#include "text.h" +#include "logging.h" +#include "screen.h" + +#define SPIRV_ENTRY_POINT "main" + +SDL_GPUDevice *ta_gpu_device; +SDL_GPUGraphicsPipeline *ta_gpu_text_pipeline; +SDL_GPUTexture *ta_gpu_text_texture; +SDL_GPUSampler *ta_gpu_text_sampler; +Uint32 ta_gpu_text_texture_width; +Uint32 ta_gpu_text_texture_height; +static SDL_GPUTexture *ta_gpu_msaa_texture; +static SDL_GPUTexture *ta_gpu_depth_texture; +SDL_GPUSampleCount ta_gpu_sample_count = SDL_GPU_SAMPLECOUNT_8; +SDL_GPUTextureFormat ta_gpu_swapchain_format; +static Uint32 ta_gpu_msaa_texture_width; +static Uint32 ta_gpu_msaa_texture_height; +static SDL_GPUCommandBuffer *ta_gpu_command_buffer; +static SDL_GPUTexture *ta_gpu_swapchain_texture; + +static void ta_gpu_resize_msaa_texture(SDL_GPUTextureFormat format) { + if (ta_gpu_msaa_texture_width == (Uint32)ta_window_width && + ta_gpu_msaa_texture_height == (Uint32)ta_window_height && + ta_gpu_msaa_texture) { + return; + } + + SDL_ReleaseGPUTexture(ta_gpu_device, ta_gpu_msaa_texture); + SDL_ReleaseGPUTexture(ta_gpu_device, ta_gpu_depth_texture); + ta_gpu_msaa_texture = NULL; + ta_gpu_depth_texture = NULL; + + SDL_GPUTextureCreateInfo texture_info = { + .type = SDL_GPU_TEXTURETYPE_2D, + .format = format, + .usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, + .width = (Uint32)ta_window_width, + .height = (Uint32)ta_window_height, + .layer_count_or_depth = 1, + .num_levels = 1, + .sample_count = ta_gpu_sample_count, + }; + ta_gpu_msaa_texture = ta_sdl_create_gpu_texture(ta_gpu_device, &texture_info); + texture_info.format = SDL_GPU_TEXTUREFORMAT_D32_FLOAT; + texture_info.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET; + ta_gpu_depth_texture = ta_sdl_create_gpu_texture(ta_gpu_device, &texture_info); + ta_gpu_msaa_texture_width = (Uint32)ta_window_width; + ta_gpu_msaa_texture_height = (Uint32)ta_window_height; +} + +static SDL_GPUShader *ta_gpu_load_shader(SDL_GPUDevice *device, const void *code, size_t code_size, SDL_GPUShaderStage stage, Uint32 num_samplers, Uint32 num_uniform_buffers) { + SDL_GPUShaderCreateInfo info = { + .code = code, + .code_size = code_size, + .entrypoint = SPIRV_ENTRY_POINT, + .format = SDL_GPU_SHADERFORMAT_SPIRV, + .stage = stage, + .num_samplers = num_samplers, + .num_uniform_buffers = num_uniform_buffers, + }; + + SDL_GPUShader *shader = ta_sdl_create_gpu_shader(device, &info); + return shader; +} + +void ta_gpu_init(void) { + ta_gpu_device = ta_sdl_create_gpu_device(); + + ta_sdl_claim_window_for_gpu_device(ta_gpu_device, ta_window); + + ta_gpu_swapchain_format = SDL_GetGPUSwapchainTextureFormat(ta_gpu_device, ta_window); + ta_gpu_resize_msaa_texture(ta_gpu_swapchain_format); + + SDL_GPUShader *vertex_shader = ta_gpu_load_shader( + ta_gpu_device, + file_text_vert_slang_spv_start, + file_text_vert_slang_spv_size, + SDL_GPU_SHADERSTAGE_VERTEX, + 0, + 1 + ); + SDL_GPUShader *fragment_shader = ta_gpu_load_shader( + ta_gpu_device, + file_text_frag_slang_spv_start, + file_text_frag_slang_spv_size, + SDL_GPU_SHADERSTAGE_FRAGMENT, + 1, + 0 + ); + + SDL_GPUColorTargetDescription color_target = { + .format = ta_gpu_swapchain_format, + .blend_state = { + .src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA, + .dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + .color_blend_op = SDL_GPU_BLENDOP_ADD, + .src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE, + .dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + .alpha_blend_op = SDL_GPU_BLENDOP_ADD, + .enable_blend = true, + }, + }; + SDL_GPUGraphicsPipelineCreateInfo pipeline_info = { + .vertex_shader = vertex_shader, + .fragment_shader = fragment_shader, + .primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .target_info = { + .num_color_targets = 1, + .color_target_descriptions = &color_target, + }, + .multisample_state = { + .sample_count = ta_gpu_sample_count, + }, + }; + ta_gpu_text_pipeline = ta_sdl_create_gpu_graphics_pipeline(ta_gpu_device, &pipeline_info); + SDL_ReleaseGPUShader(ta_gpu_device, vertex_shader); + SDL_ReleaseGPUShader(ta_gpu_device, fragment_shader); + + SDL_GPUSamplerCreateInfo sampler_info = { + .min_filter = SDL_GPU_FILTER_LINEAR, + .mag_filter = SDL_GPU_FILTER_LINEAR, + .mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST, + .address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + }; + ta_gpu_text_sampler = ta_sdl_create_gpu_sampler(ta_gpu_device, &sampler_info); +} + +void ta_gpu_start_frame(void) { + ta_gpu_command_buffer = ta_sdl_acquire_gpu_command_buffer(ta_gpu_device); + ta_gpu_swapchain_texture = NULL; + ta_gpu_swapchain_format = SDL_GetGPUSwapchainTextureFormat(ta_gpu_device, ta_window); + + ta_sdl_wait_and_acquire_gpu_swapchain_texture(ta_gpu_command_buffer, ta_window, &ta_gpu_swapchain_texture); + if (ta_gpu_swapchain_texture) { + ta_gpu_resize_msaa_texture(ta_gpu_swapchain_format); + } +} + +void ta_gpu_end_frame(void) { + if (ta_gpu_swapchain_texture) { + ta_gpu_upload_text(ta_gpu_command_buffer); + ta_screen_upload(ta_gpu_command_buffer); + + float red = 0.0f; + float green = 0.0f; + float blue = 0.0f; + float alpha = 1.0f; + + SDL_GPUColorTargetInfo color_target = { + .texture = ta_gpu_msaa_texture, + .load_op = SDL_GPU_LOADOP_CLEAR, + .store_op = SDL_GPU_STOREOP_RESOLVE, + .resolve_texture = ta_gpu_swapchain_texture, + .clear_color = { + red, + green, + blue, + alpha + }, + }; + + SDL_GPUDepthStencilTargetInfo depth_target = { + .texture = ta_gpu_depth_texture, + .clear_depth = 1.0f, + .load_op = SDL_GPU_LOADOP_CLEAR, + .store_op = SDL_GPU_STOREOP_DONT_CARE, + .stencil_load_op = SDL_GPU_LOADOP_DONT_CARE, + .stencil_store_op = SDL_GPU_STOREOP_DONT_CARE, + }; + SDL_GPURenderPass *render_pass = ta_sdl_begin_gpu_render_pass( + ta_gpu_command_buffer, + &color_target, + 1, + &depth_target + ); + + ta_text_render(ta_gpu_command_buffer, render_pass); + ta_screen_render(ta_gpu_command_buffer, render_pass); + SDL_EndGPURenderPass(render_pass); + } + + ta_sdl_submit_gpu_command_buffer(ta_gpu_command_buffer); +} diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..b5f6a2c --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,81 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ta_sdl.h" +#include "keyboard.h" +#include <stdlib.h> +#include <string.h> + +static ta_key_callback_fn *ta_keyboard_callbacks = NULL; +static int ta_keyboard_callbacks_capacity = 0; + +static int ta_keyboard_ensure_capacity(int key) { + if (key < ta_keyboard_callbacks_capacity) { + return 1; + } + + int new_capacity = ta_keyboard_callbacks_capacity; + while (new_capacity <= key) { + new_capacity += sizeof(SDLK_UNKNOWN); + } + + ta_key_callback_fn *new_array = realloc( + ta_keyboard_callbacks, + (size_t)new_capacity * sizeof(ta_key_callback_fn) + ); + if (new_array == NULL) { + return 0; + } + + memset( + new_array + ta_keyboard_callbacks_capacity, + 0, + (size_t)(new_capacity - ta_keyboard_callbacks_capacity) * sizeof(ta_key_callback_fn) + ); + + ta_keyboard_callbacks = new_array; + ta_keyboard_callbacks_capacity = new_capacity; + return 1; +} + +void ta_keyboard_set_callback(int key, void (*callback)(void)) { + if (key < 0 || !ta_keyboard_ensure_capacity(key)) { + return; + } + ta_keyboard_callbacks[key] = callback; +} + +void ta_keyboard_dispatch(int key) { + if (key < 0) { + return; + } + + if (key < ta_keyboard_callbacks_capacity && ta_keyboard_callbacks[key] != NULL) { + ta_keyboard_callbacks[key](); + } +} + +void ta_keyboard_shutdown(void) { + if (ta_keyboard_callbacks) { + free(ta_keyboard_callbacks); + } + + ta_keyboard_callbacks = NULL; + ta_keyboard_callbacks_capacity = 0; +}
\ No newline at end of file diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..cdaa6bb --- /dev/null +++ b/src/logging.c @@ -0,0 +1,227 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2010 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ta_sdl.h" + +#ifdef UNIX_LIKE +#define UNW_LOCAL_ONLY +#include <libunwind.h> +#include <elfutils/libdwfl.h> +#include <unistd.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <time.h> +#include <sys/time.h> + +#define LOG_TIMESTAMP_SIZE 30 + +static Dwfl *ta_dwfl_open_self(void) { + static char *debuginfo_path; + static const Dwfl_Callbacks callbacks = { + .find_elf = dwfl_linux_proc_find_elf, + .find_debuginfo = dwfl_standard_find_debuginfo, + .debuginfo_path = &debuginfo_path, + }; + + Dwfl *dwfl = dwfl_begin(&callbacks); + if (dwfl == NULL) { + return NULL; + } + + if (dwfl_linux_proc_report(dwfl, getpid()) != 0) { + dwfl_end(dwfl); + return NULL; + } + + if (dwfl_report_end(dwfl, NULL, NULL) != 0) { + dwfl_end(dwfl); + return NULL; + } + + return dwfl; +} + +static char *ta_get_proc_name(unw_cursor_t *cursor) { + char *buf = NULL; + size_t cap = 0; + unw_word_t proc_offset = 0; + + while (1) { + size_t grow_to = (cap == 0) ? 64 : (cap * 2); + char *grown = realloc(buf, grow_to); + if (grown == NULL) { + free(buf); + return NULL; + } + buf = grown; + cap = grow_to; + + int ret = unw_get_proc_name(cursor, buf, cap, &proc_offset); + if (ret == 0) { + return buf; + } + if (ret != -UNW_ENOMEM) { + free(buf); + return NULL; + } + } +} + +static void ta_get_timestamp(char *buf, size_t buf_size) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + + struct tm tm_info; + localtime_r(&ts.tv_sec, &tm_info); + + size_t len = strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", &tm_info); + snprintf(buf + len, buf_size - len, ".%03ld", ts.tv_nsec / 1000000); +} + +static char *ta_format_message(const char *format, va_list args) { + va_list args_copy; + va_copy(args_copy, args); + int needed = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + + if (needed < 0) { + return NULL; + } + + char *message = malloc((size_t)needed + 1); + if (message == NULL) { + return NULL; + } + + vsnprintf(message, (size_t)needed + 1, format, args); + return message; +} + +void ta_log(const char *format, ...) { + unw_cursor_t cursor; + unw_context_t context; + int code; + + code = unw_getcontext(&context); + if (code != 0) { + SDL_Log("unw_getcontext failed with code %d\n", code); + return; + } + + code = unw_init_local(&cursor, &context); + if (code != 0) { + SDL_Log("unw_init_local failed with code %d\n", code); + return; + } + + code = unw_step(&cursor); + if (code <= 0) { + SDL_Log("unw_step failed with code %d\n", code); + return; + } + + unw_word_t ip = 0; + if (unw_get_reg(&cursor, UNW_REG_IP, &ip) != 0 || ip == 0) { + SDL_Log("unw_get_reg failed to resolve caller IP\n"); + return; + } + + const char *file = NULL; + int src_line = 0; + const char *func_name = NULL; + char *proc_name_buf = NULL; + + Dwfl *dwfl = ta_dwfl_open_self(); + if (dwfl != NULL) { + Dwfl_Module *mod = dwfl_addrmodule(dwfl, (Dwarf_Addr)ip); + if (mod != NULL) { + func_name = dwfl_module_addrname(mod, (GElf_Addr)ip); + + Dwfl_Line *dwfl_line = dwfl_module_getsrc(mod, (Dwarf_Addr)ip); + if (dwfl_line != NULL) { + Dwarf_Addr src_addr; + file = dwfl_lineinfo(dwfl_line, &src_addr, &src_line, NULL, NULL, NULL); + } + } + } + + if (func_name == NULL) { + proc_name_buf = ta_get_proc_name(&cursor); + func_name = proc_name_buf; + } + + va_list args; + va_start(args, format); + char *message = ta_format_message(format, args); + va_end(args); + + const char *out_message = (message != NULL) ? message : format; + + /* + * PATCH: + * Remove leading '/' + * I cannot find a way to make it not do it with the compiler. + * This uses extra cpu cycles and makes the binary larger then it has to be. + * Make a script to trim it from the DWARF info. + */ + if (file != NULL) { + const char *basename = strrchr(file, '/'); + if (basename != NULL) { + file = basename + 1; + } + } + + char timestamp[LOG_TIMESTAMP_SIZE]; + ta_get_timestamp(timestamp, sizeof(timestamp)); + + if (file != NULL && src_line > 0 && func_name != NULL) { + printf("[%s] %s:%d:%s(): %s\n", timestamp, file, src_line, func_name, out_message); + } else if (func_name != NULL) { + printf("[%s] %s(): %s\n", timestamp, func_name, out_message); + } else { + printf("[%s] 0x%lx: %s\n", timestamp, (unsigned long)ip, out_message); + } + + free(message); + free(proc_name_buf); + + if (dwfl != NULL) { + dwfl_end(dwfl); + } +} + +#else // UNIX_LIKE + +void ta_log(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + + SDL_LogMessageV( + SDL_LOG_CATEGORY_APPLICATION, + SDL_LOG_PRIORITY_INFO, + fmt, + args + ); + + va_end(args); +} + +#endif // UNIX_LIKE
\ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4cbd53a --- /dev/null +++ b/src/main.c @@ -0,0 +1,88 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include "window.h" +#include "version.h" +#include "ta_sdl.h" +#include "logging.h" +#include "forever.h" +#include "gpu.h" +#include "text.h" +#include "events.h" +#include "exit_key.h" +#include "keyboard.h" +#include "ta_sdl_ttf.h" +#include "scale.h" +#include "audio.h" +#include "screen.h" +#include "color.h" +#include "teapot.h" +#include "mouse.h" + +static void ta_main_init(void) { + ta_color_init(); + ta_window_init(); + ta_exit_key_init(); + ta_sdl_ttf_init(); + ta_text_init(); + ta_gpu_init(); + ta_teapot_init(); +#ifdef ENABLE_AUDIO + ta_audio_init(); +#endif // ENABLE_AUDIO + ta_screen_current = MAIN_MENU; +} + +static void ta_main_update(void) { + ta_events_update(); + ta_window_update(); +#ifdef ENABLE_AUDIO + ta_audio_update(); +#endif // ENABLE_AUDIO + ta_gpu_start_frame(); + ta_mouse_update(); + ta_screen_update(); + ta_gpu_end_frame(); +} + +void ta_main_exit(int code) { + ta_keyboard_shutdown(); +#ifdef ENABLE_AUDIO + ta_audio_destroy(); +#endif // ENABLE_AUDIO + ta_text_shutdown(); + ta_teapot_shutdown(); + ta_window_shutdown(); + ta_sdl_ttf_shutdown(); + ta_sdl_shutdown(); + + ta_log("Exiting tuxanci2 with code %d", code); + exit(code); +} + +int main(void) { + SDL_Log("tuxanci2 version " TA_VERSION "\n\n"); + + ta_main_init(); + + ta_forever(ta_main_update); + + return 0; +}
\ No newline at end of file diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt deleted file mode 100644 index 50b3c79..0000000 --- a/src/modules/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -############################################################################### -# tuxanci/src/modules/CMakeLists.txt -############################################################################### -############################################################################### -# SOURCE SPECIFICATION -############################################################################### -INCLUDE_DIRECTORIES ( ${CMAKE_BINARY_DIR}/src/base/ ) -IF ( BUILD_SERVER ) - SET ( SRC_base_modules - ${WORKDIR}/server/publicServer.h - ) -ELSE ( BUILD_SERVER ) - SET ( SRC_base_modules - ${WORKDIR}/client/interface.h ${WORKDIR}/client/image.h - ) -ENDIF ( BUILD_SERVER ) -SET ( SRC_base_modules - ${SRC_base_modules} - ${WORKDIR}/base/main.h ${WORKDIR}/base/modules.h - ${WORKDIR}/base/shot.h ${WORKDIR}/base/tux.h - ${WORKDIR}/base/list ${WORKDIR}/base/gun.h - ${WORKDIR}/base/space ${WORKDIR}/base/index -) -############################################################################### -# COMPILATION AND INSTALLATION ITSELF -############################################################################### -FILE ( GLOB _lib_files mod*.c ) -FOREACH ( _current_LIB_FILE ${_lib_files} ) - GET_FILENAME_COMPONENT( _lib ${_current_LIB_FILE} NAME_WE ) - ADD_LIBRARY ( ${_lib} STATIC ${SRC_base_modules} ${_current_LIB_FILE} ) -ENDFOREACH ( _current_LIB_FILE ) -############################################################################### diff --git a/src/modules/modAI.c b/src/modules/modAI.c deleted file mode 100644 index c7d2c02..0000000 --- a/src/modules/modAI.c +++ /dev/null @@ -1,410 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "list.h" -#include "gun.h" -#include "space.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -static export_fce_t *export_fce; - -typedef struct alternative_struct { - int first; - int route; - int step; - int x, y; -} alternative_t; - -static alternative_t *newAlternative(int route, int x, int y) -{ - alternative_t *new; - - new = malloc(sizeof(alternative_t)); - new->first = route; - new->route = route; - new->x = x; - new->y = y; - new->step = 0; - - return new; -} - -static alternative_t *cloneAlternative(alternative_t *p, int route, int x, int y) -{ - alternative_t *new; - - assert(p != NULL); - - new = newAlternative(route, p->x, p->y); - new->first = p->first; - new->step = p->step; - - return new; -} - -static void forkAlternative(list_t *list, alternative_t *p, int w, int h) -{ - int x, y; - - assert(list != NULL); - assert(p != NULL); - - x = p->x; - y = p->y; - - switch (p->route) { - case TUX_UP: - list_add(list, cloneAlternative(p, TUX_RIGHT, x + (w + 5), y)); - list_add(list, cloneAlternative(p, TUX_LEFT, x - (w + 5), y)); - break; - case TUX_RIGHT: - list_add(list, cloneAlternative(p, TUX_UP, x, y - (h + 5))); - list_add(list, cloneAlternative(p, TUX_DOWN, x, y + (h + 5))); - break; - case TUX_LEFT: - list_add(list, cloneAlternative(p, TUX_UP, x, y - (h + 5))); - list_add(list, cloneAlternative(p, TUX_DOWN, x, y + (h + 5))); - break; - case TUX_DOWN: - list_add(list, cloneAlternative(p, TUX_RIGHT, x + (w + 5), y)); - list_add(list, cloneAlternative(p, TUX_LEFT, x - (w + 5), y)); - break; - } - -} - -static void moveAlternative(alternative_t *p, int offset) -{ - assert(p != NULL); - - p->step++; - - /*printf("move %d %d %d\n", p->x, p->y, p->step);*/ - - switch (p->route) { - case TUX_UP: - p->y -= offset; - break; - case TUX_RIGHT: - p->x += offset; - break; - case TUX_LEFT: - p->x -= offset; - break; - case TUX_DOWN: - p->y += offset; - break; - } -} - -static void destroyAlternative(alternative_t *p) -{ - assert(p != NULL); - free(p); -} - -static void cmd_ai(char *line) -{ -} - -static int init(export_fce_t *p) -{ - export_fce = p; - - return 0; -} - -#ifndef PUBLIC_SERVER -static int draw(int x, int y, int w, int h) -{ - return 0; -} -#endif /* PUBLIC_SERVER */ - -static tux_t *findOtherTux(space_t *space) -{ - int i; - - for (i = 0; i < space_get_count(space); i++) { - tux_t *thisTux; - - thisTux = space_get_item(space, i); - - if (thisTux->control != TUX_CONTROL_AI) { - return thisTux; - } - } - - return NULL; -} - -static void shotTux(arena_t *arena, tux_t *tux_ai, tux_t *tux_rival) -{ - const int limit = 20; - int x_ai, y_ai; - int x_rival, y_rival; - int w, h; - - export_fce->fce_tux_get_proportion(tux_ai, &x_ai, &y_ai, &w, &h); - export_fce->fce_tux_get_proportion(tux_rival, &x_rival, &y_rival, &w, &h); - - if (y_rival < y_ai && x_rival > x_ai && x_rival < x_ai + limit) { - export_fce->fce_tux_action(tux_ai, TUX_UP); - export_fce->fce_tux_action(tux_ai, TUX_SHOT); - } - - if (x_rival > x_ai && y_rival > y_ai && y_rival < y_ai + limit) { - export_fce->fce_tux_action(tux_ai, TUX_RIGHT); - export_fce->fce_tux_action(tux_ai, TUX_SHOT); - } - - if (x_rival < x_ai && y_rival > y_ai && y_rival < y_ai + limit) { - export_fce->fce_tux_action(tux_ai, TUX_LEFT); - export_fce->fce_tux_action(tux_ai, TUX_SHOT); - } - - if (y_rival > y_ai && x_rival > x_ai && x_rival < x_ai + limit) { - export_fce->fce_tux_action(tux_ai, TUX_DOWN); - export_fce->fce_tux_action(tux_ai, TUX_SHOT); - } -} - -static void tux_eventAI(tux_t *tux) -{ - arena_t *arena; - tux_t *rivalTux; - - list_t *listAlternative; - list_t *listDst; - list_t *listFork; - - int x, y, w, h; - int rival_x, rival_y; - int i, my_index; - - int countFork = 0; - int countDel = 0; - int countLimit = 0; - int countDo = 0; - - export_fce->fce_tux_get_proportion(tux, &x, &y, &w, &h); - /*printf("tux AI %d %d\n", x, y);*/ - - arena = export_fce->fce_arena_get_current(); - - rivalTux = findOtherTux(arena->spaceTux); - - if (rivalTux == NULL || rivalTux->status != TUX_STATUS_ALIVE) { - return; - } - - listAlternative = list_new(); - listDst = list_new(); - listFork = list_new(); - - shotTux(arena, tux, rivalTux); - - export_fce->fce_tux_get_proportion(rivalTux, &rival_x, &rival_y, NULL, NULL); - /*printf("tux rival %d %d\n", rival_x, rival_y);*/ - - list_add(listAlternative, newAlternative(TUX_UP, x, y - (h + 10))); - list_add(listAlternative, newAlternative(TUX_RIGHT, x + (w + 10), y)); - list_add(listAlternative, newAlternative(TUX_LEFT, x - (w + 10), y)); - list_add(listAlternative, newAlternative(TUX_DOWN, x, y + (h + 10))); - - my_index = -1; - for (;;) { - alternative_t *this; - - my_index++; - if (my_index < 0 || my_index >= listAlternative->count) { - int j; - - /*printf("listFork->count = %d\n", listFork->count);*/ - - for (j = 0; j < listFork->count; j++) { - list_add(listAlternative, listFork->list[j]); - } - - list_do_empty(listFork); - - my_index = 0; - } - - if (listAlternative->count == 0) { - break; - } - - this = (alternative_t *) listAlternative->list[my_index]; - - if (++countDo == 100) { - break; - } - - if (this->step > 100) { - list_del_item(listAlternative, my_index, destroyAlternative); - countLimit++; - my_index--; - continue; - } - - moveAlternative(this, w * 2); - - if (export_fce->fce_arena_is_free_space(arena, this->x, this->y, w, h) == 1) { - forkAlternative(listFork, this, 2 * w, 2 * h); - countFork++; - continue; - } - - if (export_fce->fce_arena_conflict_space(this->x, this->y, w, h, - rival_x, rival_y, w, h)) { - /*printf("this->step = %d\n", this->step);*/ - - list_del(listAlternative, my_index); - list_add(listDst, this); - my_index--; - /*continue;*/ - break; - } - - if (export_fce->fce_arena_is_free_space(arena, this->x, this->y, w, h) == 0) { - /*forkAlternative(listFork, this, w*2, h*2);*/ - list_del_item(listAlternative, my_index, destroyAlternative); - my_index--; - countDel++; - - continue; - } - - } - - int minStep = 1000; - int recRoute = 0; - - /*printf("------------\n");*/ - for (i = 0; i < listDst->count; i++) { - alternative_t *this; - - this = (alternative_t *) listDst->list[i]; - - /*printf("XXX step %d route = %d\n", this->step, this->first);*/ - if (this->step < minStep) { - minStep = this->step; - recRoute = this->first; - } - } - - if (recRoute != 0) { - export_fce->fce_tux_action(tux, recRoute); - } - - list_destroy_item(listFork, destroyAlternative); - list_destroy_item(listAlternative, destroyAlternative); - list_destroy_item(listDst, destroyAlternative); -/* - printf("countFork = %d\n", countFork); - printf("countDel = %d\n", countDel); - printf("countLimit = %d\n", countLimit); - printf("countDo = %d\n", countDo); -*/ -} - -static void action_tuxAI(space_t *space, tux_t *tux, void *p) -{ - if (tux->control == TUX_CONTROL_AI && tux->status == TUX_STATUS_ALIVE) { - tux_eventAI(tux); - } -} - -static int event() -{ - static my_time_t lastEvent = 0; - my_time_t curentTime; - arena_t *arena; - int countTuxAI; - /*int i;*/ - - if (lastEvent == 0) { - lastEvent = export_fce->fce_timer_get_current_time(); - } - - curentTime = export_fce->fce_timer_get_current_time(); - - if (curentTime - lastEvent < 25) { - return 0; - } - - lastEvent = export_fce->fce_timer_get_current_time(); - /*printf("event AI\n");*/ - - arena = export_fce->fce_arena_get_current(); - - if (arena == NULL) { - return 0; - } - - countTuxAI = 0; - - space_action(arena->spaceTux, action_tuxAI, NULL); -/* - for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) - { - tux_t *thisTux; - - thisTux = arena->spaceTux->list->list[i]; - - if( thisTux->control == TUX_CONTROL_AI && - thisTux->status == TUX_STATUS_ALIVE ) - { - tux_event(thisTux); - countTuxAI = 0; - } - } -*/ - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - return 0; -} - -static void cmdArena(char *line) -{ - if (strncmp(line, "ai", 2) == 0) { - cmd_ai(line); - } -} - -static void recvMsg(char *msg) -{ -} - -static int destroy() -{ - return 0; -} - -mod_sym_t modai_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/modules/modBasic.c b/src/modules/modBasic.c deleted file mode 100644 index 8494916..0000000 --- a/src/modules/modBasic.c +++ /dev/null @@ -1,84 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "list.h" -#include "gun.h" -#include "space.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -static export_fce_t *export_fce; - -static int init(export_fce_t *p) -{ - export_fce = p; - - debug("Initializing Basic module"); - return 0; -} - -#ifndef PUBLIC_SERVER -static int draw(int x, int y, int w, int h) -{ - debug("Drawing Basic module"); - return 0; -} -#endif /* PUBLIC_SERVER */ - -static int event() -{ - debug("Basic module catch event"); - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - debug("isConflict(%d, %d, %d, %d) in Basic module", x, y, w, h); - return 0; -} - -static void cmd_basic(char *line) -{ - debug("cmd_basic(%s) in Basic module", line); -} - -static void cmdArena(char *line) -{ - if (strncmp(line, "basic", 5) == 0) { - cmd_basic(line); - } -} - -static void recvMsg(char *msg) -{ - debug("recvMsg(%s) in Basic module", msg); -} - -static int destroy() -{ - debug("Destroying Basic module"); - return 0; -} - -mod_sym_t modbasic_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/modules/modMove.c b/src/modules/modMove.c deleted file mode 100644 index 9774170..0000000 --- a/src/modules/modMove.c +++ /dev/null @@ -1,269 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "list.h" -#include "gun.h" -#include "space.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -static export_fce_t *export_fce; - -static void move_tux(tux_t *tux, int x, int y, int w, int h) -{ - int dist_x = 0, dist_y = 0; - - if (tux->bonus == BONUS_GHOST || export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - switch (tux->position) { - case TUX_UP: - dist_x = x + w / 2; - dist_y = y - (TUX_HEIGHT + 20); - break; - case TUX_LEFT: - dist_x = x - (TUX_WIDTH + 20); - dist_y = y + h / 2; - break; - case TUX_RIGHT: - dist_x = x + w + 20; - dist_y = y + h / 2; - break; - case TUX_DOWN: - dist_x = x + w / 2; - dist_y = y + h + 20; - break; - default: - fatal("Variable tux->position in modMove has an undefined value [%d]", tux->position); - break; - } - - if (export_fce->fce_arena_is_free_space(export_fce->fce_arena_get_current(), dist_x, dist_y, TUX_WIDTH, TUX_HEIGHT)) { - space_move_object(export_fce->fce_arena_get_current()->spaceTux, tux, dist_x, dist_y); -#ifndef PUBLIC_SERVER - /*sound_play("teleport", SOUND_GROUP_BASE);*/ -#endif /* PUBLIC_SERVER */ - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - char msg[STR_PROTO_SIZE]; - sprintf(msg, "movetux %d %d %d", tux->id, dist_x, dist_y); - if (tux->bonus == BONUS_HIDDEN) { - export_fce->fce_proto_send_module_server(PROTO_SEND_ONE, (client_t *) tux->client, msg); - } else { - export_fce->fce_proto_send_module_server(PROTO_SEND_ALL, NULL, msg); - } - } - } -} - -static int myAbs(int n) -{ - return (n > 0 ? n : -n); -} - - -static int getSppedShot(shot_t *shot) -{ - return (myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py)); -} - -static void transformShot(shot_t *shot, int position) -{ - int speed; - - speed = getSppedShot(shot); - switch (position) { - case TUX_UP: - shot->px = 0; - shot->py = -speed; - break; - case TUX_LEFT: - shot->px = -speed; - shot->py = 0; - break; - case TUX_RIGHT: - shot->px = +speed; - shot->py = 0; - break; - case TUX_DOWN: - shot->px = 0; - shot->py = +speed; - break; - } - - shot->position = position; - shot->isCanKillAuthor = TRUE; - - if (shot->gun == GUN_LASSER) { - export_fce->fce_shot_transform_lasser(shot); - } -} - -static void move_shot(shot_t *shot, int position, int src_x, int src_y, - int dist_x, int dist_y, int dist_w, int dist_h) -{ - int offset = 0; - int new_x = 0, new_y = 0; - - switch (shot->position) { - case TUX_UP: - case TUX_DOWN: - offset = shot->x - src_x; - break; - - case TUX_RIGHT: - case TUX_LEFT: - offset = shot->y - src_y; - break; - } - - transformShot(shot, position); - - switch (shot->position) { - case TUX_UP: - new_x = dist_x + offset; - new_y = dist_y - (shot->h + 5); - break; - case TUX_LEFT: - new_x = dist_x - (shot->w + 5); - new_y = dist_y + offset; - break; - case TUX_RIGHT: - new_x = dist_x + dist_w + 5; - new_y = dist_y + offset; - break; - case TUX_DOWN: - new_x = dist_x + offset; - new_y = dist_y + dist_h + 5; - break; - } - - space_move_object(export_fce->fce_arena_get_current()->spaceShot, shot, new_x, new_y); - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - char msg[STR_PROTO_SIZE]; - - sprintf(msg, "moveshot %d %d %d %d %d %d", shot->id, - shot->x, shot->y, shot->px, shot->py, shot->position); - export_fce->fce_proto_send_module_server(PROTO_SEND_ALL, NULL, msg); - } -} - -static int init(export_fce_t *p) -{ - export_fce = p; - export_fce->fce_share_function_add("move_tux", (void *) move_tux); - export_fce->fce_share_function_add("move_shot", (void *) move_shot); - - return 0; -} - -static void proto_movetux(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int id, x, y; - space_t *space; - tux_t *tux; - - assert(msg != NULL); - - sscanf(msg, "%s %d %d %d", cmd, &id, &x, &y); - space = export_fce->fce_arena_get_current()->spaceTux; - tux = space_get_object_id(space, id); - - if (tux != NULL) { - space_move_object(space, tux, x, y); - } -} - -static void proto_moveshot(char *msg) -{ - char cmd[STR_PROTO_SIZE]; - int shot_id, x, y, px, py, position; - space_t *space; - shot_t *shot; - - assert(msg != NULL); - - sscanf(msg, "%s %d %d %d %d %d %d", cmd, &shot_id, &x, &y, &px, &py, &position); - - space = export_fce->fce_arena_get_current()->spaceShot; - - if ((shot = space_get_object_id(space, shot_id)) == NULL) { - return; - } - - space_move_object(space, shot, x, y); - shot->isCanKillAuthor = 1; - shot->position = position; - shot->px = px; - shot->py = py; - - if (shot->gun == GUN_LASSER) { - export_fce->fce_shot_transform_lasser(shot); - } -} - -#ifndef PUBLIC_SERVER -static int draw(int x, int y, int w, int h) -{ - return 0; -} -#endif /* PUBLIC_SERVER */ - -static int event() -{ - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - return 0; -} - -static void cmdArena(char *line) -{ -} - -static void recvMsg(char *msg) -{ - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - return; - } - - if (strncmp(msg, "movetux", 7) == 0) { - proto_movetux(msg); - } - - if (strncmp(msg, "moveshot", 8) == 0) { - proto_moveshot(msg); - } -} - -static int destroy() -{ - return 0; -} - -mod_sym_t modmove_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/modules/modPipe.c b/src/modules/modPipe.c deleted file mode 100644 index 9319fe0..0000000 --- a/src/modules/modPipe.c +++ /dev/null @@ -1,335 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "list.h" -#include "gun.h" -#include "space.h" -#include "proto.h" -#include "serverSendMsg.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -typedef struct pipe_struct { - /* position of the pipe */ - int x; - int y; - - /* size of the pipe */ - int w; - int h; - - int id; - int id_out; - int position; - - /* layer in the arena where the pipe lies */ - int layer; - -#ifndef PUBLIC_SERVER - /* its image */ - image_t *img; -#endif /* PUBLIC_SERVER */ -} pipe_t; - -static void (*fce_move_shot) (shot_t *shot, int position, int src_x, int src_y, int dist_x, int dist_y, int dist_w, int dist_h); - -static export_fce_t *export_fce; - -static list_t *listPipe; -static space_t *spacePipe; - -#ifndef PUBLIC_SERVER -static pipe_t *newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position, image_t *img) -#else /* PUBLIC_SERVER */ -static pipe_t *newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position) -#endif /* PUBLIC_SERVER */ -{ - pipe_t *new; - -#ifndef PUBLIC_SERVER - assert(img != NULL); -#endif /* PUBLIC_SERVER */ - - new = malloc(sizeof(pipe_t)); - assert(new != NULL); - - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->layer = layer; - new->id = id; - new->id_out = id_out; - new->position = position; -#ifndef PUBLIC_SERVER - new->img = img; -#endif /* PUBLIC_SERVER */ - return new; -} - -static void setStatusPipe(void *p, int x, int y, int w, int h) -{ - pipe_t *pipe; - - pipe = p; - - pipe->x = x; - pipe->y = y; - pipe->w = w; - pipe->h = h; -} - -static void getStatusPipe(void *p, int *id, int *x, int *y, int *w, int *h) -{ - pipe_t *pipe; - - pipe = p; - - *id = pipe->id; - *x = pipe->x; - *y = pipe->y; - *w = pipe->w; - *h = pipe->h; -} - -#ifndef PUBLIC_SERVER -static void drawPipe(pipe_t *p) -{ - assert(p != NULL); - - export_fce->fce_addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer); -} -#endif /* PUBLIC_SERVER */ - -static void destroyPipe(pipe_t *p) -{ - assert(p != NULL); - free(p); -} - -static void cmd_teleport(char *line) -{ - char str_x[STR_NUM_SIZE]; - char str_y[STR_NUM_SIZE]; - char str_w[STR_NUM_SIZE]; - char str_h[STR_NUM_SIZE]; - char str_id[STR_NUM_SIZE]; - char str_id_out[STR_NUM_SIZE]; - char str_position[STR_NUM_SIZE]; - char str_layer[STR_NUM_SIZE]; - char str_image[STR_SIZE]; - pipe_t *new; - - if (export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "id", str_id, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "id_out", str_id_out, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "position", str_position, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0) { - return; - } - -#ifndef PUBLIC_SERVER - new = newPipe(atoi(str_x), atoi(str_y), - atoi(str_w), atoi(str_h), - atoi(str_layer), atoi(str_id), atoi(str_id_out), - atoi(str_position), - export_fce->fce_image_get(IMAGE_GROUP_USER, str_image)); -#else /* PUBLIC_SERVER */ - new = newPipe(atoi(str_x), atoi(str_y), - atoi(str_w), atoi(str_h), - atoi(str_layer), atoi(str_id), atoi(str_id_out), - atoi(str_position)); -#endif /* PUBLIC_SERVER */ - - if (spacePipe == NULL) { - spacePipe = space_new(export_fce->fce_arena_get_current()->w, - export_fce->fce_arena_get_current()->h, 320, 240, - getStatusPipe, setStatusPipe); - } - - space_add(spacePipe, new); -} - -static void moveShotFromPipe(shot_t *shot, pipe_t *pipe) -{ - pipe_t *distPipe; - - distPipe = space_get_object_id(spacePipe, pipe->id_out); - - if (distPipe == NULL) { - error("Pipe ID was not found [%d]", pipe->id); - return; - } - - fce_move_shot(shot, distPipe->position, pipe->x, pipe->y, distPipe->x, distPipe->y, distPipe->w, distPipe->h); -} - -static int init(export_fce_t *p) -{ - export_fce = p; - - listPipe = list_new(); - - if (export_fce->fce_module_load_dep("libmodMove") != 0) { - return -1; - } - - if ((fce_move_shot = export_fce->fce_share_function_get("move_shot")) == NULL) { - return -1; - } - - return 0; -} - -#ifndef PUBLIC_SERVER -static void action_drawpipe(space_t *space, pipe_t *pipe, void *p) -{ - drawPipe(pipe); -} - -static int draw(int x, int y, int w, int h) -{ - if (spacePipe == NULL) { - return 0; - } - - space_action_from_location(spacePipe, action_drawpipe, NULL, x, y, w, h); - /*space_print(spacePipe);*/ - - return 0; -} -#endif /* PUBLIC_SERVER */ - -static int negPosition(int n) -{ - switch (n) { - case TUX_UP: - return TUX_DOWN; - - case TUX_LEFT: - return TUX_RIGHT; - - case TUX_RIGHT: - return TUX_LEFT; - - case TUX_DOWN: - return TUX_UP; - - default: - fatal("Tux is probably moving in another dimension"); - break; - } - - return -1; -} - -static void action_eventpipe(space_t *space, pipe_t *pipe, shot_t *shot) -{ - arena_t *arena; - tux_t *author; - - arena = export_fce->fce_arena_get_current(); - - author = space_get_object_id(arena->spaceTux, shot->author_id); - - if (author != NULL && author->bonus == BONUS_GHOST && author->bonus_time > 0) { - return; - } - - if (negPosition(shot->position) == pipe->position) { - moveShotFromPipe(shot, pipe); - } else { - if (shot->gun == GUN_BOMBBALL) { - export_fce->fce_shot_bound_bombBall(shot); - } else { - shot->del = TRUE; - } - } -} - -static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spacePipe) -{ - space_action_from_location(p_spacePipe, action_eventpipe, shot, shot->x, shot->y, shot->w, shot->h); - - if (shot->del == TRUE) { - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_SERVER) { - export_fce->fce_proto_send_del_server(PROTO_SEND_ALL, NULL, shot->id); - } - - space_del_with_item(space, shot, export_fce->fce_shot_destroy); - } -} - -static int event() -{ - if (spacePipe == NULL) { - return 0; - } - - /* just for a test */ - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return 0; - } - - space_action(export_fce->fce_arena_get_current()->spaceShot, action_eventshot, spacePipe); - - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - if (spacePipe == NULL) { - return 0; - } - - return space_is_conflict_with_object(spacePipe, x, y, w, h); -} - -static void cmdArena(char *line) -{ - if (strncmp(line, "pipe", 4) == 0) { - cmd_teleport(line); - } -} - -static void recvMsg(char *msg) -{ -} - -static int destroy() -{ - space_destroy_with_item(spacePipe, destroyPipe); - spacePipe = NULL; - - list_destroy(listPipe); - listPipe = NULL; - - return 0; -} - -mod_sym_t modpipe_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/modules/modTeleport.c b/src/modules/modTeleport.c deleted file mode 100644 index c8cf573..0000000 --- a/src/modules/modTeleport.c +++ /dev/null @@ -1,343 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "list.h" -#include "gun.h" -#include "space.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -typedef struct teleport_struct { - int id; - - /* position of the teleport */ - int x; - int y; - - /* size of the teleport */ - int w; - int h; - - /* layer in the arena where the teleport lies */ - int layer; - -#ifndef PUBLIC_SERVER - /* its image */ - image_t *img; -#endif /* PUBLIC_SERVER */ -} teleport_t; - -static export_fce_t *export_fce; - -static space_t *spaceTeleport; -static list_t *listTeleport; - -static void (*fce_move_tux) (tux_t *tux, int x, int y, int w, int h); -static void (*fce_move_shot) (shot_t *shot, int position, int src_x, - int src_y, int dist_x, int dist_y, int dist_w, - int dist_h); - -#ifndef PUBLIC_SERVER -static teleport_t *newTeleport(int x, int y, int w, int h, int layer, image_t *img) -#else /* PUBLIC_SERVER */ -static teleport_t *newTeleport(int x, int y, int w, int h, int layer) -#endif /* PUBLIC_SERVER */ -{ - static int last_id = 0; - - teleport_t *new; - -#ifndef PUBLIC_SERVER - assert(img != NULL); -#endif /* PUBLIC_SERVER */ - - new = malloc(sizeof(teleport_t)); - assert(new != NULL); - - new->id = ++last_id; - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->layer = layer; -#ifndef PUBLIC_SERVER - new->img = img; -#endif /* PUBLIC_SERVER */ - - return new; -} - -static void setStatusTeleport(void *p, int x, int y, int w, int h) -{ - teleport_t *teleport; - - teleport = p; - - teleport->x = x; - teleport->y = y; - teleport->w = w; - teleport->h = h; -} - -static void getStatusTeleport(void *p, int *id, int *x, int *y, int *w, int *h) -{ - teleport_t *teleport; - teleport = p; - - *id = teleport->id; - *x = teleport->x; - *y = teleport->y; - *w = teleport->w; - *h = teleport->h; -} - -#ifndef PUBLIC_SERVER -static void drawTeleport(teleport_t *p) -{ - assert(p != NULL); - - export_fce->fce_addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer); -} -#endif /* PUBLIC_SERVER */ - -static void destroyTeleport(teleport_t *p) -{ - assert(p != NULL); - free(p); -} - -static void cmd_teleport(char *line) -{ - char str_x[STR_NUM_SIZE]; - char str_y[STR_NUM_SIZE]; - char str_w[STR_NUM_SIZE]; - char str_h[STR_NUM_SIZE]; - char str_layer[STR_NUM_SIZE]; - char str_image[STR_SIZE]; - teleport_t *new; - - if (export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0) { - return; - } - -#ifndef PUBLIC_SERVER - new = newTeleport(atoi(str_x), atoi(str_y), - atoi(str_w), atoi(str_h), - atoi(str_layer), - export_fce->fce_image_get(IMAGE_GROUP_USER, str_image)); -#else /* PUBLIC_SERVER */ - new = newTeleport(atoi(str_x), atoi(str_y), - atoi(str_w), atoi(str_h), - atoi(str_layer)); -#endif /* PUBLIC_SERVER */ - - if (spaceTeleport == NULL) { - spaceTeleport = space_new(export_fce->fce_arena_get_current()->w, - export_fce->fce_arena_get_current()->h, - 320, 240, - getStatusTeleport, setStatusTeleport); - } - - space_add(spaceTeleport, new); -} - -static teleport_t *getRandomTeleportBut(space_t *space, teleport_t *teleport) -{ - int my_index; - - do { - my_index = random() % space_get_count(space); - } while (space_get_item(space, my_index) == teleport); - - return (teleport_t *) space_get_item(space, my_index); -} - -static int getRandomPosition() -{ - switch (random() % 4) { - case 0: - return TUX_UP; - - case 1: - return TUX_LEFT; - - case 2: - return TUX_RIGHT; - - case 3: - return TUX_DOWN; - } - - fatal("Generated a random number which is not in range 0 to 3"); - - return -1; -} - -static void teleportTux(tux_t *tux, teleport_t *teleport) -{ - teleport_t *distTeleport; - - if (tux->bonus == BONUS_GHOST || - export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return; - } - - distTeleport = getRandomTeleportBut(spaceTeleport, teleport); - - fce_move_tux(tux, distTeleport->x, distTeleport->y, distTeleport->w, distTeleport->h); -} - -static void moveShotFromTeleport(shot_t *shot, teleport_t *teleport, space_t *space) -{ - teleport_t *distTeleport; - - distTeleport = getRandomTeleportBut(space, teleport); - - fce_move_shot(shot, getRandomPosition(), teleport->x, teleport->y, - distTeleport->x, distTeleport->y, distTeleport->w, - distTeleport->h); -} - -static int init(export_fce_t *p) -{ - export_fce = p; - - listTeleport = list_new(); - - if (export_fce->fce_module_load_dep("libmodMove") != 0 || - (fce_move_tux = export_fce->fce_share_function_get("move_tux")) == NULL || - (fce_move_shot = export_fce->fce_share_function_get("move_shot")) == NULL) { - return -1; - } - - return 0; -} - -#ifndef PUBLIC_SERVER -static void action_drawteleport(space_t *space, teleport_t *teleport, void *p) -{ - drawTeleport(teleport); -} - -static int draw(int x, int y, int w, int h) -{ - if (spaceTeleport == NULL) { - return 0; - } - - space_action_from_location(spaceTeleport, action_drawteleport, NULL, x, y, w, h); - - return 0; -} -#endif /* PUBLIC_SERVER */ - -static void action_eventteleportshot(space_t *space, teleport_t *teleport, shot_t *shot) -{ - arena_t *arena; - tux_t *author; - - arena = export_fce->fce_arena_get_current(); - - author = space_get_object_id(arena->spaceTux, shot->author_id); - - if (author != NULL && author->bonus == BONUS_GHOST && author->bonus_time > 0) { - return; - } - - moveShotFromTeleport(shot, teleport, spaceTeleport); -} - -static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spaceTeleport) -{ - space_action_from_location(p_spaceTeleport, action_eventteleportshot, - shot, shot->x, shot->y, shot->w, shot->h); -} - -static void action_eventteleporttux(space_t *space, teleport_t *teleport, tux_t *tux) -{ - teleportTux(tux, teleport); -} - -static void action_eventtux(space_t *space, tux_t *tux, space_t *p_spaceTeleport) -{ - int x, y, w, h; - - export_fce->fce_tux_get_proportion(tux, &x, &y, &w, &h); - - space_action_from_location(p_spaceTeleport, action_eventteleporttux, tux, x, y, w, h); -} - -static int event() -{ - if (spaceTeleport == NULL) { - return 0; - } - - if (export_fce->fce_net_multiplayer_get_game_type() == NET_GAME_TYPE_CLIENT) { - return 0; - } - - space_action(export_fce->fce_arena_get_current()->spaceShot, action_eventshot, spaceTeleport); - space_action(export_fce->fce_arena_get_current()->spaceTux, action_eventtux, spaceTeleport); - - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - if (spaceTeleport == NULL) { - return 0; - } - - return 0; -} - -static void cmdArena(char *line) -{ - if (strncmp(line, "teleport", 8) == 0) { - cmd_teleport(line); - } -} - -static void recvMsg(char *msg) -{ -} - -static int destroy() -{ - space_destroy_with_item(spaceTeleport, destroyTeleport); - spaceTeleport = NULL; - - list_destroy(listTeleport); - listTeleport = NULL; - - return 0; -} - -mod_sym_t modteleport_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/modules/modWall.c b/src/modules/modWall.c deleted file mode 100644 index 7a1fa7a..0000000 --- a/src/modules/modWall.c +++ /dev/null @@ -1,336 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "modules.h" -#include "tux.h" -#include "shot.h" -#include "gun.h" -#include "space.h" - -#ifndef PUBLIC_SERVER -#include "interface.h" -#include "image.h" -#else /* PUBLIC_SERVER */ -#include "publicServer.h" -#endif /* PUBLIC_SERVER */ - -typedef struct wall_struct { - int id; - - /* position of the teleport */ - int x; - int y; - - /* size of the teleport */ - int w; - int h; - - int img_x; - int img_y; - - /* layer in the arena where the teleport lies */ - int layer; - -#ifndef PUBLIC_SERVER - /* its image */ - image_t *img; -#endif /* PUBLIC_SERVER */ -} wall_t; - -static export_fce_t *export_fce; - -static space_t *spaceWall; - -#ifndef PUBLIC_SERVER -static space_t *spaceImgWall; -#endif /* PUBLIC_SERVER */ - -#ifndef PUBLIC_SERVER -static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer, image_t *img) -#else /* PUBLIC_SERVER */ -static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer) -#endif /* PUBLIC_SERVER */ -{ - static int last_id = 0; - wall_t *new; - -#ifndef PUBLIC_SERVER - assert(img != NULL); -#endif /* PUBLIC_SERVER */ - new = malloc(sizeof(wall_t)); - assert(new != NULL); - - new->id = ++last_id; - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->img_x = img_x; - new->img_y = img_y; - new->layer = layer; -#ifndef PUBLIC_SERVER - new->img = img; -#endif /* PUBLIC_SERVER */ - - return new; -} - -#ifndef PUBLIC_SERVER -static void drawWall(wall_t *p) -{ - assert(p != NULL); - - export_fce->fce_addLayer(p->img, - p->img_x, p->img_y, - 0, 0, - p->img->w, p->img->h, - p->layer); -} -#endif /* PUBLIC_SERVER */ - -static void destroyWall(wall_t *p) -{ - assert(p != NULL); - free(p); -} - -static void getStatusWall(void *p, int *id, int *x, int *y, int *w, int *h) -{ - wall_t *wall; - - wall = p; - - *id = wall->id; - *x = wall->x; - *y = wall->y; - *w = wall->w; - *h = wall->h; -} - -static void setStatusWall(void *p, int x, int y, int w, int h) -{ - wall_t *wall; - - wall = p; - - wall->x = x; - wall->y = y; - wall->w = w; - wall->h = h; -} - -#ifndef PUBLIC_SERVER -static void getStatusImgWall(void *p, int *id, int *x, int *y, int *w, int *h) -{ - wall_t *wall; - - wall = p; - - *id = wall->id; - *x = wall->img_x; - *y = wall->img_y; - *w = wall->img->w; - *h = wall->img->h; -} - -static void setStatusImgWall(void *p, int x, int y, int w, int h) -{ -} -#endif /* PUBLIC_SERVER */ - -static void cmd_wall(char *line) -{ - char str_x[STR_NUM_SIZE]; - char str_y[STR_NUM_SIZE]; - char str_img_x[STR_NUM_SIZE]; - char str_img_y[STR_NUM_SIZE]; - char str_w[STR_NUM_SIZE]; - char str_h[STR_NUM_SIZE]; - char str_layer[STR_NUM_SIZE]; - char str_image[STR_SIZE]; - char str_rel[STR_SIZE]; - int x, y, w, h, img_x, img_y, layer; - int rel; - wall_t *new; - - rel = 0; - - if (export_fce->fce_getValue(line, "rel", str_rel, STR_NUM_SIZE) != 0) { - strcpy(str_rel, "0"); - } - - if (export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "img_x", str_img_x, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "img_y", str_img_y, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 || - export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0) { - return; - } - - rel = atoi(str_rel); - x = atoi(str_x); - y = atoi(str_y); - w = atoi(str_w); - h = atoi(str_h); - img_x = atoi(str_img_x); - img_y = atoi(str_img_y); - layer = atoi(str_layer); - - if (rel == 1) { - img_x += x; - img_y += y; - } -#ifndef PUBLIC_SERVER - new = newWall(x, y, w, h, img_x, img_y, layer, export_fce->fce_image_get(IMAGE_GROUP_USER, str_image)); -#else /* PUBLIC_SERVER */ - new = newWall(x, y, w, h, img_x, img_y, layer); -#endif /* PUBLIC_SERVER */ - - if (spaceWall == NULL) { - spaceWall = space_new(export_fce->fce_arena_get_current()->w, - export_fce->fce_arena_get_current()->h, - 320, 240, - getStatusWall, setStatusWall); - } - -#ifndef PUBLIC_SERVER - if (spaceImgWall == NULL) { - spaceImgWall = space_new(export_fce->fce_arena_get_current()->w, - export_fce->fce_arena_get_current()->h, - 320, 240, - getStatusImgWall, setStatusImgWall); - } -#endif /* PUBLIC_SERVER */ - - space_add(spaceWall, new); - -#ifndef PUBLIC_SERVER - space_add(spaceImgWall, new); -#endif /* PUBLIC_SERVER */ -} - -static int init(export_fce_t *p) -{ - export_fce = p; - - return 0; -} - -#ifndef PUBLIC_SERVER -static void action_drawwall(space_t *space, wall_t *wall, void *p) -{ - drawWall(wall); -} - -static int draw(int x, int y, int w, int h) -{ - if (spaceWall == NULL) { - return 0; - } - - space_action_from_location(spaceImgWall, action_drawwall, NULL, x, y, w, h); - - /*space_print(spaceWall);*/ - - return 0; -} -#endif /* PUBLIC_SERVER */ - -static void action_eventwall(space_t *space, wall_t *wall, shot_t *shot) -{ - arena_t *arena; - tux_t *author; - - arena = export_fce->fce_arena_get_current(); - - author = space_get_object_id(arena->spaceTux, shot->author_id); - - if (author != NULL && author->bonus == BONUS_GHOST && author->bonus_time > 0) { - return; - } - - if (shot->gun == GUN_BOMBBALL) { - if (export_fce->fce_net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - export_fce->fce_shot_bound_bombBall(shot); - } - - return; - } - - /*space_del_with_item(arena->spaceShot, shot, export_fce->fce_shot_destroy);*/ - shot->del = TRUE; -} - -static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spaceWall) -{ - space_action_from_location(p_spaceWall, action_eventwall, - shot, shot->x, shot->y, - shot->w, shot->h); - - if (shot->del == TRUE) { - space_del_with_item(space, shot, export_fce->fce_shot_destroy); - } -} - -static int event() -{ - if (spaceWall == NULL) { - return 0; - } - - space_action(export_fce->fce_arena_get_current()->spaceShot, - action_eventshot, spaceWall); - - return 0; -} - -static int isConflict(int x, int y, int w, int h) -{ - if (spaceWall == NULL) { - return 0; - } - - return space_is_conflict_with_object(spaceWall, x, y, w, h); -} - -static void cmdArena(char *line) -{ - if (strncmp(line, "wall", 4) == 0) { - cmd_wall(line); - } -} - -static void recvMsg(char *msg) -{ -} - -static int destroy() -{ - space_destroy_with_item(spaceWall, destroyWall); -#ifndef PUBLIC_SERVER - space_destroy(spaceImgWall); - spaceImgWall = NULL; -#endif /* PUBLIC_SERVER */ - - spaceWall = NULL; - - return 0; -} - -mod_sym_t modwall_sym = { &init, -#ifndef PUBLIC_SERVER - &draw, -#else /* PUBLIC_SERVER */ - 0, -#endif /* PUBLIC_SERVER */ - &event, - &isConflict, - &cmdArena, - &recvMsg, - &destroy }; diff --git a/src/mouse.c b/src/mouse.c new file mode 100644 index 0000000..2ae3229 --- /dev/null +++ b/src/mouse.c @@ -0,0 +1,35 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "mouse.h" +#include "ta_sdl.h" + +int ta_mouse_x; +int ta_mouse_y; +bool ta_mouse_click; + +void ta_mouse_update(void) { + float x; + float y; + SDL_MouseButtonFlags buttons = SDL_GetMouseState(&x, &y); + + ta_mouse_x = (int)x; + ta_mouse_y = (int)y; + ta_mouse_click = (buttons & SDL_BUTTON_LMASK) != 0; +}
\ No newline at end of file diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/net/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/net/dns.c b/src/net/dns.c deleted file mode 100644 index 83853e2..0000000 --- a/src/net/dns.c +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __WIN32__ -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#else -#include <windows.h> -#include <wininet.h> -#endif - -#include <string.h> -#include "main.h" -#include "dns.h" - -char *dns_resolv(char *domain) -{ - struct hostent *host; - - host = gethostbyname(domain); - - if (host == NULL || host->h_addr_list[0] == NULL) { - return NULL; - } - - return strdup(inet_ntoa(*((struct in_addr *) host->h_addr_list[0]))); -} diff --git a/src/net/dns.h b/src/net/dns.h deleted file mode 100644 index 082c01f..0000000 --- a/src/net/dns.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef DNS_H -#define DNS_H - -extern char *dns_resolv(char *domain); - -#endif /* DNS_H */ diff --git a/src/net/tcp.c b/src/net/tcp.c deleted file mode 100644 index 20ab3aa..0000000 --- a/src/net/tcp.c +++ /dev/null @@ -1,337 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#ifndef __WIN32__ -#include <sys/types.h> -#include <sys/time.h> -#include <sys/socket.h> -#include <sys/select.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <fcntl.h> -#else /* __WIN32__ */ -#include <windows.h> -#include <wininet.h> -#endif /* __WIN32__ */ - -#include <unistd.h> -#include <assert.h> - -#include "tcp.h" - -sock_tcp_t *sock_tcp_new(void) -{ - sock_tcp_t *new; - - new = malloc(sizeof(sock_tcp_t)); - memset(new, 0, sizeof(sock_tcp_t)); - - return new; -} - -void sock_tcp_destroy(sock_tcp_t *p) -{ - assert(p != NULL); - free(p); -} - -static int getProto(char *str) -{ - if (strstr(str, ".") != NULL) - return PROTO_TCPv4; - if (strstr(str, ":") != NULL) - return PROTO_TCPv6; - - fatal("Network protocol not detected"); - return -1; -} - -sock_tcp_t *sock_tcp_bind(char *address, int port) -{ - sock_tcp_t *new; - unsigned long param_setsock = 1; - int len; - int ret; - - assert(port > 0 && port < 65535); - - new = sock_tcp_new(); - new->proto = getProto(address); - ret = -1; /* no warnings */ - - assert(new != NULL); - - if (new->proto == PROTO_TCPv4) { - new->sock = socket(AF_INET, SOCK_STREAM, 0); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_TCPv6) { - new->sock = socket(AF_INET6, SOCK_STREAM, 0); - } -#endif - - if (new->sock < 0) { - error("Unable to create TCP socket"); - sock_tcp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) ¶m_setsock, sizeof(param_setsock)); - - if (new->proto == PROTO_TCPv4) { - new->sockAddr.sin_family = AF_INET; -#ifndef __WIN32__ - inet_pton(AF_INET, address, &(new->sockAddr.sin_addr)); -#else - new->sockAddr.sin_addr.s_addr = inet_addr(address); -#endif - new->sockAddr.sin_port = htons(port); - - len = sizeof(new->sockAddr); - ret = bind(new->sock, (struct sockaddr *) &new->sockAddr, len); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_TCPv6) { - new->sockAddr6.sin6_family = AF_INET6; - /*new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);*/ -#ifndef __WIN32__ - inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); -#else /* __WIN32__ */ - new->sockAddr6.sin6_addr.s_addr = inet_addr(address); -#endif /* __WIN32__ */ - new->sockAddr6.sin6_port = htons(port); - - len = sizeof(new->sockAddr6); - ret = bind(new->sock, (struct sockaddr *) &new->sockAddr6, len); - } -#endif /* SUPPORT_IPv6 */ - - if (ret < 0) { - error("Unable to bind to TCP port [%d]", port); - sock_tcp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - listen(new->sock, 5); - - return new; -} - -sock_tcp_t *sock_tcp_accept(sock_tcp_t *p) -{ - sock_tcp_t *new; - int client_len; - - assert(p != NULL); - assert(p->sock >= 0); - - new = sock_tcp_new(); - new->proto = p->proto; - - if (new->proto == PROTO_TCPv4) { - client_len = sizeof(new->sockAddr); -#ifndef __WIN32__ - new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr, (socklen_t *) &client_len); -#else - new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr, (long *) &client_len); -#endif - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_TCPv6) { - client_len = sizeof(new->sockAddr6); -#ifndef __WIN32__ - new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr6, (socklen_t *) &client_len); -#else /* __WIN32__ */ - new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr6, (long *) &client_len); -#endif /* __WIN32__ */ - } -#endif /* SUPPORT_IPv6 */ - - if (new->sock < 0) { - sock_tcp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - return new; -} - -void sock_tcp_get_ip(sock_tcp_t *p, char *str_ip, int len) -{ - assert(p != NULL); - assert(str_ip != NULL); - - if (p->proto == PROTO_TCPv4) { -#ifndef __WIN32__ - inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len); -#else - strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); -#endif - } -#ifdef SUPPORT_IPv6 - if (p->proto == PROTO_TCPv6) { -#ifndef __WIN32__ - inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len); -#else /* __WIN32__ */ - /* NOT TESTED */ - strcpy(str_ip, inet_ntoa(p->sockAddr6.sin6_addr)); -#endif /* __WIN32__ */ - } -#endif /* SUPPORT_IPv6 */ -} - -int sock_tcp_get_port(sock_tcp_t *p) -{ - assert(p != NULL); - - if (p->proto == PROTO_TCPv4) { - return htons(p->sockAddr.sin_port); - } -#ifdef SUPPORT_IPv6 - if (p->proto == PROTO_TCPv6) { - return htons(p->sockAddr6.sin6_port); - } -#endif - - fatal("Bad IP protocol"); - - return -1; -} - -sock_tcp_t *sock_tcp_connect(char *ip, int port) -{ - sock_tcp_t *new; - int len; - int ret; - - assert(ip != NULL); - assert(port > 0 && port < 65535); - - new = sock_tcp_new(); - new->proto = getProto(ip); - ret = -1; /* no warnings */ - - if (new->proto == PROTO_TCPv4) { - new->sock = socket(AF_INET, SOCK_STREAM, 0); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_TCPv6) { - new->sock = socket(AF_INET6, SOCK_STREAM, 0); - } -#endif - - if (new->sock < 0) { - error("Unable to create TCP socket for connecting"); - sock_tcp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - if (new->proto == PROTO_TCPv4) { - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_addr.s_addr = inet_addr(ip); - new->sockAddr.sin_port = htons(port); - - len = sizeof(new->sockAddr); - ret = connect(new->sock, (struct sockaddr *) &new->sockAddr, len); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_TCPv6) { - new->sockAddr6.sin6_family = AF_INET6; - inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr)); - new->sockAddr6.sin6_port = htons(port); - - len = sizeof(new->sockAddr6); - ret = connect(new->sock, (struct sockaddr *) &new->sockAddr6, len); - } -#endif - - if (ret < 0) { - error("Unable to connect to [%s]:%d", ip, port); - sock_tcp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - return new; -} - -int sock_tcp_disable_nagle(sock_tcp_t *p) -{ - int flag = 1; - int result; - - result = setsockopt(p->sock, /* affected socket */ - IPPROTO_TCP, /* set option at TCP level */ - TCP_NODELAY, /* name of the option */ - (char *) &flag, /* the cast is a historical cruft */ - sizeof(int)); /* length of the option value */ - - return result; -} - -int sock_tcp_set_non_block(sock_tcp_t *p) -{ - /* set to nonblocking socket mode */ -#ifndef __WIN32__ - int oldFlag; - - oldFlag = fcntl(p->sock, F_GETFL, 0); - - if (fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) { - return -1; - } -#else - unsigned long arg = 1; - /* operation is FIONBIO; parameter is a pointer on non-zero number */ - if (ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR) { - WSACleanup(); - return -1; - } -#endif - return 0; -} - -int sock_tcp_read(sock_tcp_t *p, void *address, int len) -{ - assert(p != NULL); - assert(address != NULL); - - return read(p->sock, address, len); -} - -int sock_tcp_write(sock_tcp_t *p, void *address, int len) -{ - assert(p != NULL); - assert(address != NULL); - - return write(p->sock, address, len); -} - -void sock_tcp_close(sock_tcp_t *p) -{ - assert(p != NULL); - - close(p->sock); - sock_tcp_destroy(p); -#ifdef __WIN32__ - WSACleanup(); -#endif -} diff --git a/src/net/tcp.h b/src/net/tcp.h deleted file mode 100644 index 20d4136..0000000 --- a/src/net/tcp.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef MY_TCP_H -#define MY_TCP_H - -#include "main.h" - -#ifndef __WIN32__ -#include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#else -#include <windows.h> -#include <wininet.h> -#endif - -#define PROTO_TCPv4 0 -#define PROTO_TCPv6 1 - -typedef struct struct_sock_tcp_t { -#ifndef __WIN32_ - int sock; -#else - SOCKET sock; -#endif - int proto; - - struct sockaddr_in sockAddr; -# ifdef SUPPORT_IPv6 - struct sockaddr_in6 sockAddr6; -# endif -} sock_tcp_t; - -extern sock_tcp_t *sock_tcp_new(void); -extern void sock_tcp_destroy(sock_tcp_t *p); -extern sock_tcp_t *sock_tcp_bind(char *addresss, int port); -extern sock_tcp_t *sock_tcp_accept(sock_tcp_t *p); -extern void sock_tcp_get_ip(sock_tcp_t *p, char *str_ip, int len); -extern int sock_tcp_get_port(sock_tcp_t *p); -extern sock_tcp_t *sock_tcp_connect(char *ip, int port); -extern int sock_tcp_disable_nagle(sock_tcp_t *p); -extern int sock_tcp_set_non_block(sock_tcp_t *p); -extern int sock_tcp_read(sock_tcp_t *p, void *address, int len); -extern int sock_tcp_write(sock_tcp_t *p, void *address, int len); -extern void sock_tcp_close(sock_tcp_t *p); - -#endif /* MY_TCP_H */ diff --git a/src/net/udp.c b/src/net/udp.c deleted file mode 100644 index 1771222..0000000 --- a/src/net/udp.c +++ /dev/null @@ -1,328 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#ifndef __WIN32__ -#include <sys/socket.h> -#include <sys/types.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#else -#include <windows.h> -#include <wininet.h> -#endif - -#include <unistd.h> -#include <assert.h> -#include <fcntl.h> - -#include "main.h" -#include "net_multiplayer.h" - -#include "udp.h" -#include "dns.h" - -#define BUFSIZE 1000 - -static int getProto(char *str) -{ - if (strstr(str, ".") != NULL) - return PROTO_UDPv4; - if (strstr(str, ":") != NULL) - return PROTO_UDPv6; - - fatal("Network protocol not detected"); - return -1; -} - -sock_udp_t *sock_udp_new(void) -{ - sock_udp_t *new; - - new = malloc(sizeof(sock_udp_t)); - memset(new, 0, sizeof(sock_udp_t)); - - return new; -} - -void sock_udp_destroy(sock_udp_t *p) -{ - assert(p != NULL); - free(p); -} - -sock_udp_t *sock_udp_bind(char *address, int port) -{ - sock_udp_t *new; - int res = -1; /* no warnings */ - - assert(port > 0 && port < 65535); - - new = sock_udp_new(); - new->proto = getProto(address); - - assert(new != NULL); - - if (new->proto == PROTO_UDPv4) { - new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_UDPv6) { - new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - } -#endif - - if (new->sock < 0) { - error("Unable to create UDP socket"); - sock_udp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); - - if (new->proto == PROTO_UDPv4) { - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_port = htons(port); - new->sockAddr.sin_addr.s_addr = inet_addr(address); - - res = bind(new->sock, (struct sockaddr *) &(new->sockAddr), sizeof(new->sockAddr)); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_UDPv6) { - new->sockAddr6.sin6_family = AF_INET6; - new->sockAddr6.sin6_port = htons(port); - inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); - - res = bind(new->sock, (struct sockaddr *) &(new->sockAddr6), sizeof(new->sockAddr6)); - } -#endif - - if (res < 0) { - error("Unable to bind UDP socket to [%s]:%d", address, port); - sock_udp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - return new; -} - -sock_udp_t *sock_udp_connect(char *address, int port) -{ - sock_udp_t *new; - char *domain; - - assert(address != NULL); - assert(port > 0 && port < 65536); - - new = sock_udp_new(); - new->proto = getProto(address); - - assert(new != NULL); - - if (new->proto == PROTO_UDPv4) { - new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_UDPv6) { - new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - } -#endif - - if (new->sock < 0) { - error("Unable to create UDP socket for connecting"); - sock_udp_destroy(new); -#ifdef __WIN32__ - WSACleanup(); -#endif - return NULL; - } - - domain = dns_resolv(address); - - if (domain) { - address = domain; - } - - memset(&(new->sockAddr), 0, sizeof(new->sockAddr)); - - if (new->proto == PROTO_UDPv4) { - new->sockAddr.sin_family = AF_INET; - new->sockAddr.sin_port = htons(port); - new->sockAddr.sin_addr.s_addr = inet_addr(address); - } -#ifdef SUPPORT_IPv6 - if (new->proto == PROTO_UDPv6) { - new->sockAddr6.sin6_family = AF_INET6; - new->sockAddr6.sin6_port = htons(port); - inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); - } -#endif - if (domain) { - free(domain); - } - - return new; -} - -int sock_udp_set_non_block(sock_udp_t *p) -{ - /* set to nonblocking socket mode */ -#ifndef __WIN32__ - int oldFlag; - - oldFlag = fcntl(p->sock, F_GETFL, 0); - - if (fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) { - return -1; - } -#else - unsigned long arg = 1; - /* operation is FIONBIO; parameter is a pointer on non-zero number */ - if (ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR) { - WSACleanup(); - return -1; - } -#endif - return 0; -} - -int sock_udp_read(sock_udp_t *src, sock_udp_t *dst, void *address, int len) -{ - int addrlen; - int size = -1; /* no warnings */ - - assert(src != NULL); - assert(dst != NULL); - assert(address != NULL); - - if (src->proto == PROTO_UDPv4) { - addrlen = sizeof(src->sockAddr); - -#ifndef __WIN32 - size = recvfrom(src->sock, address, len, 0, (struct sockaddr *) &dst->sockAddr, (socklen_t *) &addrlen); -#else - size = recvfrom(src->sock, address, len, 0, (struct sockaddr *) &dst->sockAddr, (int *) &addrlen); -#endif - } -#ifdef SUPPORT_IPv6 - if (src->proto == PROTO_UDPv6) { - addrlen = sizeof(src->sockAddr6); - - size = recvfrom(src->sock, address, len, 0, (struct sockaddr *) &dst->sockAddr6, (socklen_t *) &addrlen); - } -#endif - - dst->sock = src->sock; - dst->proto = src->proto; - - if (size < 0) { - char str_ip[STR_IP_SIZE]; - - sock_udp_get_ip(dst, str_ip, STR_IP_SIZE); - - error("Unable to read from UDP socket [%s]:%d (read %d bytes)", str_ip, sock_udp_get_port(dst), size); - -#ifdef __WIN32__ - WSACleanup(); -#endif - return -1; - } - - return size; -} - -int sock_udp_write(sock_udp_t *src, sock_udp_t *dst, void *address, int len) -{ - int addrlen; - int size = -1; /* no warnings */ - - assert(src != NULL); - assert(dst != NULL); - assert(address != NULL); - - if (src->proto == PROTO_UDPv4) { - addrlen = sizeof(src->sockAddr); - - size = sendto(src->sock, address, len, 0, (struct sockaddr *) &dst->sockAddr, addrlen); - } -#ifdef SUPPORT_IPv6 - if (src->proto == PROTO_UDPv6) { - addrlen = sizeof(src->sockAddr6); - - size = sendto(src->sock, address, len, 0, (struct sockaddr *) &dst->sockAddr6, addrlen); - } -#endif - - if (size < 0) { - char str_ip[STR_IP_SIZE]; - - sock_udp_get_ip(dst, str_ip, STR_IP_SIZE); - - error("Unable to write to socket [%s]:%d with protocol %d (written %d bytes)", str_ip, sock_udp_get_port(dst), dst->proto, size); - -#ifdef __WIN32__ - WSACleanup(); -#endif - return -1; - } - - return size; -} - -void sock_udp_get_ip(sock_udp_t *p, char *str_ip, int len) -{ - assert(p != NULL); - assert(str_ip != NULL); - - - if (p->proto == PROTO_UDPv4) { -#ifndef __WIN32__ - inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len); -#else - strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); -#endif - } -#ifdef SUPPORT_IPv6 - if (p->proto == PROTO_UDPv6) { - inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len); - } -#endif -} - -int sock_udp_get_port(sock_udp_t *p) -{ - assert(p != NULL); - - if (p->proto == PROTO_UDPv4) { - return htons(p->sockAddr.sin_port); - } -#ifdef SUPPORT_IPv6 - if (p->proto == PROTO_UDPv6) { - return htons(p->sockAddr6.sin6_port); - } -#endif - - fatal("Bad IP protocol"); - - return -1; -} - -void sock_udp_close(sock_udp_t *p) -{ - assert(p != NULL); - -#ifndef __WIN32__ - close(p->sock); -#else - closesocket(p->sock); - /*WSACleanup(); -- when the socket is closed correctly, it is not needed to do a cleanup */ -#endif - sock_udp_destroy(p); -} diff --git a/src/net/udp.h b/src/net/udp.h deleted file mode 100644 index 0e54a9c..0000000 --- a/src/net/udp.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef MY_UDP_H -#define MY_UDP_H - -#ifndef __WIN32__ -#include <netinet/in.h> -#else -#include <windows.h> -#include <wininet.h> -#include <winsock.h> -#endif - -typedef struct struct_sock_udp_t { -#ifndef __WIN32__ - int sock; -#else - SOCKET sock; -#endif - int proto; - -#ifdef SUPPORT_IPv6 - struct sockaddr_in6 sockAddr6; -#endif - struct sockaddr_in sockAddr; -} sock_udp_t; - -extern sock_udp_t *sock_udp_new(void); -extern void sock_udp_destroy(sock_udp_t *p); -extern sock_udp_t *sock_udp_bind(char *address, int port); -extern sock_udp_t *sock_udp_connect(char *address, int port); -extern int sock_udp_set_non_block(sock_udp_t *p); -extern int sock_udp_read(sock_udp_t *src, sock_udp_t *dst, void *address, int len); -extern int sock_udp_write(sock_udp_t *src, sock_udp_t *dst, void *address, int len); -extern void sock_udp_get_ip(sock_udp_t *p, char *str_ip, int len); -extern int sock_udp_get_port(sock_udp_t *p); -extern void sock_udp_close(sock_udp_t *p); - -#endif /* MY_UDP_H */ diff --git a/src/obj.c b/src/obj.c new file mode 100644 index 0000000..7f3442c --- /dev/null +++ b/src/obj.c @@ -0,0 +1,175 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdbool.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "obj.h" + +typedef struct ta_obj_position { + float value[3]; +} ta_obj_position; + +typedef struct ta_obj_normal { + float value[3]; +} ta_obj_normal; + +static bool ta_obj_push_vertex(ta_obj_mesh *mesh, size_t *capacity, const ta_obj_vertex *vertex) { + if (mesh->vertex_count == *capacity) { + size_t next_capacity = *capacity == 0 ? 1024 : *capacity * 2; + ta_obj_vertex *vertices = realloc(mesh->vertices, next_capacity * sizeof(*vertices)); + if (!vertices) { + return false; + } + mesh->vertices = vertices; + *capacity = next_capacity; + } + mesh->vertices[mesh->vertex_count++] = *vertex; + return true; +} + +bool ta_obj_parse(const unsigned char *data, size_t size, ta_obj_mesh *mesh) { + ta_obj_position *positions = NULL; + ta_obj_normal *normals = NULL; + size_t position_count = 0; + size_t normal_count = 0; + size_t position_capacity = 0; + size_t normal_capacity = 0; + size_t vertex_capacity = 0; + bool success = false; + + memset(mesh, 0, sizeof(*mesh)); + char *text = malloc(size + 1); + if (!text) { + return false; + } + memcpy(text, data, size); + text[size] = '\0'; + + char *line = text; + while (line && *line) { + char *next_line = strchr(line, '\n'); + if (next_line) { + *next_line = '\0'; + } + + if (line[0] == 'v' && line[1] == ' ') { + ta_obj_position position; + if (sscanf(line + 2, "%f %f %f", &position.value[0], &position.value[1], &position.value[2]) != 3) { + goto done; + } + if (position_count == position_capacity) { + size_t next_capacity = position_capacity == 0 ? 1024 : position_capacity * 2; + ta_obj_position *grown = realloc(positions, next_capacity * sizeof(*grown)); + if (!grown) { + goto done; + } + positions = grown; + position_capacity = next_capacity; + } + positions[position_count++] = position; + } else if (line[0] == 'v' && line[1] == 'n' && line[2] == ' ') { + ta_obj_normal normal; + if (sscanf(line + 3, "%f %f %f", &normal.value[0], &normal.value[1], &normal.value[2]) != 3) { + goto done; + } + if (normal_count == normal_capacity) { + size_t next_capacity = normal_capacity == 0 ? 1024 : normal_capacity * 2; + ta_obj_normal *grown = realloc(normals, next_capacity * sizeof(*grown)); + if (!grown) { + goto done; + } + normals = grown; + normal_capacity = next_capacity; + } + normals[normal_count++] = normal; + } else if (line[0] == 'f' && line[1] == ' ') { + int position_indices[3]; + int normal_indices[3]; + bool has_normals = sscanf(line + 2, "%d//%d %d//%d %d//%d", + &position_indices[0], &normal_indices[0], + &position_indices[1], &normal_indices[1], + &position_indices[2], &normal_indices[2]) == 6; + if (!has_normals && sscanf(line + 2, "%d %d %d", + &position_indices[0], &position_indices[1], &position_indices[2]) != 3) { + goto done; + } + for (int index = 0; index < 3; index++) { + if (position_indices[index] <= 0 || + (size_t)position_indices[index] > position_count || + (has_normals && (normal_indices[index] <= 0 || + (size_t)normal_indices[index] > normal_count))) { + goto done; + } + } + float face_normal[3] = {0.0f, 0.0f, 1.0f}; + if (!has_normals) { + const float *first = positions[position_indices[0] - 1].value; + const float *second = positions[position_indices[1] - 1].value; + const float *third = positions[position_indices[2] - 1].value; + float edge_a[3] = {second[0] - first[0], second[1] - first[1], second[2] - first[2]}; + float edge_b[3] = {third[0] - first[0], third[1] - first[1], third[2] - first[2]}; + face_normal[0] = edge_a[1] * edge_b[2] - edge_a[2] * edge_b[1]; + face_normal[1] = edge_a[2] * edge_b[0] - edge_a[0] * edge_b[2]; + face_normal[2] = edge_a[0] * edge_b[1] - edge_a[1] * edge_b[0]; + float length = sqrtf(face_normal[0] * face_normal[0] + face_normal[1] * face_normal[1] + face_normal[2] * face_normal[2]); + if (length > 0.0f) { + face_normal[0] /= length; + face_normal[1] /= length; + face_normal[2] /= length; + } + } + for (int index = 0; index < 3; index++) { + ta_obj_vertex vertex; + memcpy(vertex.position, positions[position_indices[index] - 1].value, sizeof(vertex.position)); + if (has_normals) { + memcpy(vertex.normal, normals[normal_indices[index] - 1].value, sizeof(vertex.normal)); + } else { + memcpy(vertex.normal, face_normal, sizeof(vertex.normal)); + } + if (!ta_obj_push_vertex(mesh, &vertex_capacity, &vertex)) { + goto done; + } + } + } + + if (!next_line) { + break; + } + line = next_line + 1; + } + + success = mesh->vertex_count > 0; +done: + if (!success) { + ta_obj_free(mesh); + } + free(positions); + free(normals); + free(text); + return success; +} + +void ta_obj_free(ta_obj_mesh *mesh) { + free(mesh->vertices); + mesh->vertices = NULL; + mesh->vertex_count = 0; +}
\ No newline at end of file diff --git a/src/obj_model.c b/src/obj_model.c new file mode 100644 index 0000000..44cf8aa --- /dev/null +++ b/src/obj_model.c @@ -0,0 +1,180 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <math.h> +#include <string.h> +#include "obj_model.h" +#include "ta_sdl.h" +#include "files.h" + +typedef struct ta_obj_uniforms { + float mvp[16]; + float model[16]; +} ta_obj_uniforms; + +static SDL_GPUShader *ta_obj_shader(SDL_GPUDevice *device, const void *code, size_t size, SDL_GPUShaderStage stage) { + SDL_GPUShaderCreateInfo info = { + .code = code, + .code_size = size, + .entrypoint = "main", + .format = SDL_GPU_SHADERFORMAT_SPIRV, + .stage = stage, + .num_uniform_buffers = stage == SDL_GPU_SHADERSTAGE_VERTEX ? 1 : 0, + }; + return ta_sdl_create_gpu_shader(device, &info); +} + +static void ta_obj_identity(float *matrix) { + for (int index = 0; index < 16; index++) matrix[index] = index % 5 == 0 ? 1.0f : 0.0f; +} + +static void ta_obj_multiply(float *result, const float *left, const float *right) { + float product[16]; + for (int column = 0; column < 4; column++) for (int row = 0; row < 4; row++) { + product[column * 4 + row] = 0.0f; + for (int inner = 0; inner < 4; inner++) product[column * 4 + row] += left[inner * 4 + row] * right[column * 4 + inner]; + } + memcpy(result, product, sizeof(product)); +} + +bool ta_obj_model_init(ta_obj_model *model, SDL_GPUDevice *device, const unsigned char *data, size_t size, SDL_GPUTextureFormat color_format, SDL_GPUTextureFormat depth_format, SDL_GPUSampleCount sample_count) { + memset(model, 0, sizeof(*model)); + if (!ta_obj_parse(data, size, &model->mesh) || model->mesh.vertex_count == 0) return false; + float minimum[3]; + float maximum[3]; + memcpy(minimum, model->mesh.vertices[0].position, sizeof(minimum)); + memcpy(maximum, minimum, sizeof(maximum)); + for (size_t index = 1; index < model->mesh.vertex_count; index++) for (int axis = 0; axis < 3; axis++) { + if (model->mesh.vertices[index].position[axis] < minimum[axis]) minimum[axis] = model->mesh.vertices[index].position[axis]; + if (model->mesh.vertices[index].position[axis] > maximum[axis]) maximum[axis] = model->mesh.vertices[index].position[axis]; + } + float center[3] = {(minimum[0] + maximum[0]) * 0.5f, (minimum[1] + maximum[1]) * 0.5f, (minimum[2] + maximum[2]) * 0.5f}; + float extent = maximum[0] - minimum[0]; + for (int axis = 1; axis < 3; axis++) if (maximum[axis] - minimum[axis] > extent) extent = maximum[axis] - minimum[axis]; + for (size_t index = 0; index < model->mesh.vertex_count; index++) for (int axis = 0; axis < 3; axis++) model->mesh.vertices[index].position[axis] = (model->mesh.vertices[index].position[axis] - center[axis]) * (2.2f / extent); + + SDL_GPUBufferCreateInfo buffer_info = {.usage = SDL_GPU_BUFFERUSAGE_VERTEX, .size = (Uint32)(model->mesh.vertex_count * sizeof(*model->mesh.vertices))}; + model->vertex_buffer = SDL_CreateGPUBuffer(device, &buffer_info); + if (!model->vertex_buffer) { + ta_obj_free(&model->mesh); + return false; + } + model->vertex_count = (Uint32)model->mesh.vertex_count; + SDL_GPUShader *vertex_shader = ta_obj_shader(device, file_obj_vert_slang_spv_start, file_obj_vert_slang_spv_size, SDL_GPU_SHADERSTAGE_VERTEX); + SDL_GPUShader *fragment_shader = ta_obj_shader(device, file_obj_frag_slang_spv_start, file_obj_frag_slang_spv_size, SDL_GPU_SHADERSTAGE_FRAGMENT); + SDL_GPUVertexBufferDescription vertex_buffer = {.slot = 0, .pitch = sizeof(ta_obj_vertex), .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX}; + SDL_GPUVertexAttribute attributes[2] = { + {.location = 0, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = 0}, + {.location = 1, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = sizeof(float) * 3}, + }; + SDL_GPUColorTargetDescription color_target = {.format = color_format}; + SDL_GPUGraphicsPipelineCreateInfo pipeline_info = { + .vertex_shader = vertex_shader, + .fragment_shader = fragment_shader, + .vertex_input_state = {.vertex_buffer_descriptions = &vertex_buffer, .num_vertex_buffers = 1, .vertex_attributes = attributes, .num_vertex_attributes = 2}, + .primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .rasterizer_state = {.cull_mode = SDL_GPU_CULLMODE_NONE}, + .depth_stencil_state = {.compare_op = SDL_GPU_COMPAREOP_LESS, .enable_depth_test = true, .enable_depth_write = true}, + .multisample_state = {.sample_count = sample_count}, + .target_info = {.num_color_targets = 1, .color_target_descriptions = &color_target, .depth_stencil_format = depth_format, .has_depth_stencil_target = true}, + }; + model->pipeline = ta_sdl_create_gpu_graphics_pipeline(device, &pipeline_info); + SDL_ReleaseGPUShader(device, vertex_shader); + SDL_ReleaseGPUShader(device, fragment_shader); + return model->pipeline != NULL; +} + +void ta_obj_model_upload(ta_obj_model *model, SDL_GPUCommandBuffer *command_buffer, SDL_GPUDevice *device) { + if (model->uploaded || !model->vertex_buffer) return; + Uint32 size = (Uint32)(model->mesh.vertex_count * sizeof(*model->mesh.vertices)); + SDL_GPUTransferBufferCreateInfo transfer_info = {.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = size}; + SDL_GPUTransferBuffer *transfer_buffer = ta_sdl_create_gpu_transfer_buffer(device, &transfer_info); + void *mapped = ta_sdl_map_gpu_transfer_buffer(device, transfer_buffer, false); + memcpy(mapped, model->mesh.vertices, size); + SDL_UnmapGPUTransferBuffer(device, transfer_buffer); + SDL_GPUCopyPass *copy_pass = ta_sdl_begin_gpu_copy_pass(command_buffer); + SDL_GPUTransferBufferLocation source = {.transfer_buffer = transfer_buffer}; + SDL_GPUBufferRegion destination = {.buffer = model->vertex_buffer, .size = size}; + SDL_UploadToGPUBuffer(copy_pass, &source, &destination, false); + SDL_EndGPUCopyPass(copy_pass); + SDL_ReleaseGPUTransferBuffer(device, transfer_buffer); + ta_obj_free(&model->mesh); + model->uploaded = true; +} + +void ta_obj_model_render(const ta_obj_model *model, SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass, int window_width, int window_height) { + if (!model->pipeline || !model->vertex_buffer || !model->uploaded) return; + ta_obj_uniforms uniforms; + float view[16]; + float projection[16] = {0}; + float view_model[16]; + float rotation_x[16]; + float rotation_y[16]; + float rotation_z[16]; + float rotation_xy[16]; + float cosine = cosf(model->rotation[0]); + float sine = sinf(model->rotation[0]); + ta_obj_identity(rotation_x); + rotation_x[5] = cosine; + rotation_x[6] = sine; + rotation_x[9] = -sine; + rotation_x[10] = cosine; + cosine = cosf(model->rotation[1]); + sine = sinf(model->rotation[1]); + ta_obj_identity(rotation_y); + rotation_y[0] = cosine; + rotation_y[2] = -sine; + rotation_y[8] = sine; + rotation_y[10] = cosine; + cosine = cosf(model->rotation[2]); + sine = sinf(model->rotation[2]); + ta_obj_identity(rotation_z); + rotation_z[0] = cosine; + rotation_z[1] = sine; + rotation_z[4] = -sine; + rotation_z[5] = cosine; + ta_obj_multiply(rotation_xy, rotation_y, rotation_x); + ta_obj_multiply(uniforms.model, rotation_z, rotation_xy); + uniforms.model[12] = model->position[0]; + uniforms.model[13] = model->position[1]; + uniforms.model[14] = model->position[2]; + float aspect = (float)window_width / (float)window_height; + float focal_length = 1.0f / tanf(22.5f * 0.01745329252f); + ta_obj_identity(view); + view[14] = -5.0f; + projection[0] = focal_length / aspect; + projection[5] = focal_length; + projection[10] = -101.0f / 99.0f; + projection[11] = -1.0f; + projection[14] = -200.0f / 99.0f; + ta_obj_multiply(view_model, view, uniforms.model); + ta_obj_multiply(uniforms.mvp, projection, view_model); + SDL_PushGPUVertexUniformData(command_buffer, 0, &uniforms, sizeof(uniforms)); + SDL_BindGPUGraphicsPipeline(render_pass, model->pipeline); + SDL_GPUBufferBinding binding = {.buffer = model->vertex_buffer}; + SDL_BindGPUVertexBuffers(render_pass, 0, &binding, 1); + SDL_DrawGPUPrimitives(render_pass, model->vertex_count, 1, 0, 0); +} + +void ta_obj_model_destroy(ta_obj_model *model, SDL_GPUDevice *device) { + SDL_ReleaseGPUGraphicsPipeline(device, model->pipeline); + SDL_ReleaseGPUBuffer(device, model->vertex_buffer); + ta_obj_free(&model->mesh); + memset(model, 0, sizeof(*model)); +}
\ No newline at end of file diff --git a/src/scale.c b/src/scale.c new file mode 100644 index 0000000..4aeea28 --- /dev/null +++ b/src/scale.c @@ -0,0 +1,32 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "window.h" + +int X(float x) { + return x * ta_window_height; +} + +int Y(float y) { + return y * ta_window_width; +} + +int S(float scale) { + return scale * ta_window_height; +}
\ No newline at end of file diff --git a/src/screen.c b/src/screen.c new file mode 100644 index 0000000..3497e52 --- /dev/null +++ b/src/screen.c @@ -0,0 +1,55 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "screen.h" +#include "screens/main_menu.h" + +ta_screens ta_screen_current = NONE; + +void ta_screen_update(void) { + switch (ta_screen_current) { + case NONE: { + break; + } + case MAIN_MENU: { + ta_screens_main_menu_update(); + break; + } + } +} + +void ta_screen_upload(SDL_GPUCommandBuffer *command_buffer) { + switch (ta_screen_current) { + case MAIN_MENU: + ta_screens_main_menu_upload(command_buffer); + break; + case NONE: + break; + } +} + +void ta_screen_render(SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass) { + switch (ta_screen_current) { + case MAIN_MENU: + ta_screens_main_menu_render(command_buffer, render_pass); + break; + case NONE: + break; + } +}
\ No newline at end of file diff --git a/src/screen/CMakeLists.txt b/src/screen/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/screen/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/screen/analyze.c b/src/screen/analyze.c deleted file mode 100644 index 450da41..0000000 --- a/src/screen/analyze.c +++ /dev/null @@ -1,186 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#endif - -#include "mainMenu.h" -#include "analyze.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" - -static widget_t *image_backgorund; - -static list_t *listWidgetLabelName; -static list_t *listWidgetLabelScore; -static list_t *listAnalyze; -static widget_t *widgetLabelMsg; - -static widget_t *button_ok; - -static analyze_t *newAnalyze(char *name, int score) -{ - analyze_t *new; - - new = malloc(sizeof(analyze_t)); - new->name = strdup(name); - new->score = score; - - return new; -} - -static void destroyAnalyze(analyze_t *p) -{ - free(p->name); - free(p); -} - -static void hotkey_escape() -{ - screen_set("mainMenu"); -} - -void analyze_start() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void analyze_draw() -{ - widget_t *this; - int i; - - wid_image_draw(image_backgorund); - - for (i = 0; i < listWidgetLabelName->count; i++) { - this = (widget_t *) listWidgetLabelName->list[i]; - label_draw(this); - } - - for (i = 0; i < listWidgetLabelScore->count; i++) { - this = (widget_t *) listWidgetLabelScore->list[i]; - label_draw(this); - } - - label_draw(widgetLabelMsg); - button_draw(button_ok); -} - -void analyze_event() -{ - button_event(button_ok); -} - -void analyze_stop() -{ - label_destroy(widgetLabelMsg); - widgetLabelMsg = label_new("", WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER); - - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_ok) { - screen_set("mainMenu"); - } -} - -void analyze_restart() -{ - list_destroy_item(listWidgetLabelName, label_destroy); - list_destroy_item(listWidgetLabelScore, label_destroy); - list_destroy_item(listAnalyze, destroyAnalyze); - - listWidgetLabelName = list_new(); - listWidgetLabelScore = list_new(); - listAnalyze = list_new(); -} - -void analyze_add(char *name, int score) -{ - list_add(listAnalyze, newAnalyze(name, score)); -} - -void analyze_set_msg(char *msg) -{ - label_destroy(widgetLabelMsg); - widgetLabelMsg = label_new(msg, WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER); -} - -void analyze_end() -{ - int i; - - for (i = 0; i < listAnalyze->count; i++) { - analyze_t *this; - char *str; - - this = (analyze_t *) listAnalyze->list[i]; - - list_add(listWidgetLabelName, label_new(this->name, - 100, 200 + 20 * i, - WIDGET_LABEL_LEFT)); - - str = getString(this->score); - - list_add(listWidgetLabelScore, label_new(str, - WINDOW_SIZE_X - 100, 200 + 20 * i, - WIDGET_LABEL_RIGHT)); - - free(str); - } -} - -void analyze_init() -{ - image_t *image; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - listWidgetLabelName = list_new(); - listWidgetLabelScore = list_new(); - listAnalyze = list_new(); - - button_ok = button_new(_("OK"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2, - WINDOW_SIZE_Y - 100, eventWidget); - - screen_register(screen_new("analyze", analyze_start, analyze_event, - analyze_draw, analyze_stop)); - - widgetLabelMsg = label_new("", WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER); -} - -void analyze_quit() -{ - wid_image_destroy(image_backgorund); - - list_destroy_item(listWidgetLabelName, label_destroy); - list_destroy_item(listWidgetLabelScore, label_destroy); - list_destroy_item(listAnalyze, destroyAnalyze); - - button_destroy(button_ok); - label_destroy(widgetLabelMsg); -} diff --git a/src/screen/analyze.h b/src/screen/analyze.h deleted file mode 100644 index 2fc2b7b..0000000 --- a/src/screen/analyze.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef SCREEN_ANALYZE_H -#define SCREEN_ANALYZE_H - -#include "main.h" - -typedef struct analyze_struct { - char *name; - int score; -} analyze_t; - -extern void analyze_start(); -extern void analyze_draw(); -extern void analyze_event(); -extern void analyze_stop(); -extern void analyze_restart(); -extern void analyze_add(char *name, int score); -extern void analyze_set_msg(char *msg); -extern void analyze_end(); -extern void analyze_init(); -extern void analyze_quit(); - -#endif /* SCREEN_ANALYZE_H */ diff --git a/src/screen/browser.c b/src/screen/browser.c deleted file mode 100644 index c826ce2..0000000 --- a/src/screen/browser.c +++ /dev/null @@ -1,759 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "homeDirector.h" -#include "net_multiplayer.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef __WIN32__ -#include <sys/socket.h> -#include <sys/select.h> -#include <sys/types.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#else /* __WIN32__ */ -#include <windows.h> -#include <wininet.h> -#endif /* __WIN32__ */ - -#include <unistd.h> -#include <fcntl.h> -#include <errno.h> - -#include "udp.h" -#include "dns.h" - -extern int errno; - -#ifndef NO_SOUND -#include "music.h" -#endif /* NO_SOUND */ - -#include "gameType.h" -#include "browser.h" - -#include "widget.h" -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_select.h" - -#define SERVER_TIMEOUT 200 - -static widget_t *image_backgorund; - -static widget_t *button_play; -static widget_t *button_back; -static widget_t *button_refresh; -static widget_t *button_reload; - -static widget_t *label_server; -static widget_t *label_version; -static widget_t *label_address; -static widget_t *label_arena; -static widget_t *label_players; -static widget_t *label_ping; - -static widget_t *select_server; - -static server_t server_list; - -/* function prototypes */ -static int LoadServers(); -static int RefreshServers(); -static server_t *server_getcurr(); -static void clear_server_list(); - -static void hotkey_escape() -{ - screen_set("gameType"); -} - -void browser_start() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif /* NO_SOUND */ - - hot_key_register(SDLK_ESCAPE, hotkey_escape); - - LoadServers(); -} - -void browser_draw() -{ - wid_image_draw(image_backgorund); - - label_draw(label_server); - label_draw(label_version); - label_draw(label_address); - label_draw(label_arena); - label_draw(label_players); - label_draw(label_ping); - - select_draw(select_server); - - button_draw(button_play); - button_draw(button_back); - button_draw(button_refresh); - button_draw(button_reload); -} - -void browser_event() -{ - select_event(select_server); - - button_event(button_play); - button_event(button_back); - button_event(button_refresh); - button_event(button_reload); -} - -void browser_stop() -{ - select_remove_all(select_server); - - hot_key_unregister(SDLK_ESCAPE); - - clear_server_list(); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_play) { - server_t *server = server_getcurr(); - - if (!server) { - return; - } - - /* server is offline */ - if (!server->state) { - return; - } - - struct in_addr srv; - srv.s_addr = server->ip; - - char *address = (char *) inet_ntoa(srv); - - setSettingGameType(NET_GAME_TYPE_CLIENT); - game_type_set_ip(address); - game_type_set_port(server->port); - - screen_set("world"); - } - - if (button == button_back) { - screen_set("gameType"); - } - - if (button == button_refresh) { - RefreshServers(); - } - - if (button == button_reload) { - LoadServers(); - } -} - -server_t *server_getcurr() -{ - int i = 0; - server_t *server; - - for (server = server_list.next; server != &server_list; server = server->next) { - if (i == select_get_index(select_server)) { - return server; - } - - i++; - } - - return 0; -} - -int server_getinfo(server_t *server) -{ - struct sockaddr_in srv; - int mySocket; - - if ((mySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - return -1; - } -#ifndef __WIN32__ - /* let's make the socket non-blocking */ - int oldFlag = fcntl(mySocket, F_GETFL, 0); - if (fcntl(mySocket, F_SETFL, oldFlag | O_NONBLOCK) == -1) { - return -1; - } -#else /* __WIN32__ */ - unsigned long arg = 1; - /* Operation is FIONBIO. Parameter is pointer on non-zero number */ - if (ioctlsocket(mySocket, FIONBIO, &arg) == SOCKET_ERROR) { - WSACleanup(); - return -1; - } -#endif /* __WIN32__ */ - - struct timeval tv; - - fd_set myset; - FD_ZERO(&myset); - FD_SET(mySocket, &myset); - - tv.tv_sec = 2; - tv.tv_usec = 0; - - srv.sin_family = AF_INET; - srv.sin_port = htons(server->port); - srv.sin_addr.s_addr = server->ip; - - if (connect(mySocket, (struct sockaddr *) &srv, sizeof(srv)) == -1) { -#ifndef __WIN32__ - if (errno != EINPROGRESS) { - return -1; - } -#else /* __WIN32__ */ - if (WSAGetLastError() != WSAEWOULDBLOCK) { - WSACleanup(); - return -1; - } -#endif /* __WIN32__ */ - } - - int ret = select(mySocket + 1, NULL, &myset, NULL, &tv); - - if (ret == -1) { - return 0; - } - - if (ret == 0) { - return -2; - } - - char request[8]; - memcpy(request, "status\n", 7); - - int r = send(mySocket, request, 7, 0); - - if (r == -1) { - return -2; - } - - FD_ZERO(&myset); - FD_SET(mySocket, &myset); - tv.tv_sec = 1; - tv.tv_usec = 0; - - char *str = (char *) malloc(sizeof(char) * 257); - - if (!str) { - return -1; - } - - while (1) { - int sel = select(mySocket + 1, &myset, NULL, NULL, &tv); - - if (sel == 0) { - free(str); -#ifndef __WIN32__ - close(mySocket); -#else /* __WIN32__ */ - closesocket(mySocket); - WSACleanup(); -#endif /* __WIN32__ */ - return -2; - } - - if (sel == -1) { - free(str); -#ifndef __WIN32__ - close(mySocket); -#else /* __WIN32__ */ - closesocket(mySocket); - WSACleanup(); -#endif /* __WIN32__ */ - return -2; - } - - if ((ret = recv(mySocket, str, 256, 0)) == -1) { - free(str); -#ifndef __WIN32__ - close(mySocket); -#else /* __WIN32__ */ - closesocket(mySocket); - WSACleanup(); -#endif /* __WIN32__ */ - return -1; - } - - if (ret > 0) { - break; - } - } - -#ifndef __WIN32__ - close(mySocket); -#else /* __WIN32__ */ - closesocket(mySocket); - WSACleanup(); -#endif /* __WIN32__ */ - - server->ping = 1000 - (tv.tv_usec / 1000); - /* - ["name: F\n"] >= svn82 - "version: svnX\n" - "clients: N\n" - "maxclients: M\n" - "uptime: D\n" - */ - - int i = 0; - - while (i < ret) { - if (str[i] == '\n') { - str[i] = '\0'; - } - - i++; - } - - unsigned name = 0; - - /* get server's name */ - if (strstr(str, "name: ")) { - name = 1; - } - - int len = 0; - - if (name) { - len = strlen(str); - if (!strncmp(str, "name: ", 6)) { - unsigned name_len = strlen(str + 6); - server->name = (char *) malloc(sizeof(char) * (name_len + 2)); - - if (!server->name) { - return 0; - } - - memcpy(server->name, str + 6, name_len + 1); - server->name[name_len + 1] = '\0'; - } - } - - /* HACK */ - if (!name) { - len = -1; - } - - /* get server's version */ - if (!strncmp(str + len + 1, "version: ", 9)) { - unsigned ver_len = strlen(str + len + 10); - server->version = (char *) malloc(sizeof(char) * (ver_len + 2)); - - if (!server->version) { - return 0; - } - - memcpy(server->version, str + len + 10, ver_len + 1); - server->version[ver_len + 1] = '\0'; - } - - len += strlen(str + len + 1) + 1; - - /* get server's connected clients count */ - if (!strncmp(str + len + 1, "clients: ", 9)) { - server->clients = atoi(str + len + 10); - } - - len += strlen(str + len + 1) + 1; - - /* get server's maximum connected clients count */ - if (!strncmp(str + len + 1, "maxclients: ", 12)) { - server->maxclients = atoi(str + len + 13); - } - - len += strlen(str + len + 1) + 1; - - /* get server's uptime */ - if (!strncmp(str + len + 1, "uptime: ", 8)) { - server->uptime = atoi(str + len + 9); - } - - len += strlen(str + len + 1) + 1; - - /* get server's arena */ - if (!strncmp(str + len + 1, "arena: ", 7)) { - unsigned arena_len = strlen(str + len + 9); - server->arena = (char *) malloc(sizeof(char) * (arena_len + 2)); - - if (!server->arena) { - return 0; - } - - memcpy(server->arena, str + len + 8, arena_len + 1); - server->arena[arena_len + 1] = '\0'; - } - - - free(str); - - server->state = 1; - - return 1; -} - -/* - * Get server list - */ -static int LoadServers() -{ -#ifndef __WIN32__ - int s; -#else /* __WIN32__ */ - SOCKET s; -#endif /* __WIN32__ */ - - char buf[1025]; - - /* first of all ensure the server list is empty */ - clear_server_list(); - select_remove_all(select_server); - - /* TODO: TCP macro */ - struct sockaddr_in server; - char *master_server_ip; - - master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN); - - if (master_server_ip == NULL) { - warning("Did not obtained IP of the MasterServer"); - return -1; - } - - server.sin_family = AF_INET; - server.sin_port = htons(NET_MASTER_PORT); - server.sin_addr.s_addr = inet_addr(master_server_ip); - - free(master_server_ip); - - if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - warning("Unable to open socket for connection to MasterServer"); - return -1; - } - - /* Set to nonblocking socket mode */ -#ifndef __WIN32__ - int oldFlag = fcntl(s, F_GETFL, 0); - if (fcntl(s, F_SETFL, oldFlag | O_NONBLOCK) == -1) { - return -1; - } -#else /* __WIN32__ */ - unsigned long arg = 1; - /* Operation is FIONBIO. Parameter is pointer on non-zero number. */ - if (ioctlsocket(s, FIONBIO, &arg) == SOCKET_ERROR) { - WSACleanup(); - return -1; - } -#endif /* __WIN32__ */ - - if (connect(s, (struct sockaddr *) &server, sizeof(server)) == -1) { -#ifndef __WIN32__ - if (errno != EINPROGRESS) { - return -1; - } -#else /* __WIN32__ */ - if (WSAGetLastError() != WSAEWOULDBLOCK) { - WSACleanup(); - return -1; - } -#endif /* __WIN32__ */ - } - - struct timeval tv; - - fd_set myset; - - FD_ZERO(&myset); - FD_SET(s, &myset); - - tv.tv_sec = 3; - tv.tv_usec = 0; - - int ret = select(s + 1, NULL, &myset, NULL, &tv); - - if (ret == -1) { - return 0; - } else if (ret == 0) { - return -2; - } - - /* send request for server list */ - send(s, "l", 1, 0); - - FD_ZERO(&myset); - FD_SET(s, &myset); - - ret = select(s + 1, &myset, NULL, NULL, &tv); - - if (ret == -1) { - return 0; - } else if (ret == 0) { - return -2; - } - - int len = recv(s, buf, 1024, 0); - -#ifndef __WIN32__ - close(s); -#else /* __WIN32__ */ - closesocket(s); - WSACleanup(); -#endif /* __WIN32__ */ - - if (len <= 0) { - return 0; - } - - int i = 0; - char list[256]; - - struct in_addr srv; - - typedef struct { - unsigned port; - unsigned ip; - } response_head; - - for (;;) { - response_head *header = (response_head *) ((char *) buf + i); - - server_t *ctx; - - /* alloc and init context */ - ctx = (server_t *) malloc(sizeof(server_t)); - - ctx->ip = header->ip; - ctx->port = header->port; - ctx->name = 0; - ctx->version = 0; - ctx->arena = 0; - - ret = server_getinfo(ctx); - - srv.s_addr = ctx->ip; - - char *address = (char *) inet_ntoa(srv); - - memset(list, ' ', 254); - list[255] = '\0'; - - if (ret == 1) { - /*sprintf (list, "%s (%s) - %s:%d - %s - %d/%d - %dms", ctx->name ? ctx->name : "Unknown", ctx->version, address, ctx->port, - ctx->arena ? ctx->arena : "Unknown", ctx->clients, ctx->maxclients, ctx->ping); */ - - if (ctx->name) { - memcpy(list, ctx->name, strlen(ctx->name)); - } else { - memcpy(list, "Unknown", 7); - } - - memcpy(list + 30, ctx->version, strlen(ctx->version)); - - unsigned a_len = strlen(address); - memcpy(list + 38, address, a_len); - memcpy(list + 38 + a_len, ":", 1); - - sprintf(buf, "%d", ctx->port); - - memcpy(list + 39 + a_len, buf, strlen(buf)); - - memcpy(list + 60, ctx->arena, strlen(ctx->arena)); - - sprintf(buf, "%d/%d", ctx->clients, ctx->maxclients); - - memcpy(list + 75, buf, strlen(buf)); - - sprintf(buf, "%dms", ctx->ping); - - memcpy(list + 83, buf, strlen(buf)); - } - - select_add(select_server, list); - - /* add server to server list */ - ctx->next = &server_list; - ctx->prev = server_list.prev; - ctx->prev->next = ctx; - ctx->next->prev = ctx; - - i += sizeof(response_head) + 1; - - if (buf[i - 1] != '\n' || (i + 1) > len) { - break; - } - } - - return 1; -} - -static int RefreshServers() -{ - select_remove_all(select_server); - - struct in_addr srv; - - server_t *server; - for (server = server_list.next; server != &server_list; server = server->next) { - if (server->name) { - free(server->name); - } - - if (server->version) { - free(server->version); - } - - if (server->arena) { - free(server->arena); - } - - server->name = 0; - server->version = 0; - server->arena = 0; - - char list[256]; - char buf[32]; - - int ret = server_getinfo(server); - - srv.s_addr = server->ip; - - char *address = (char *) inet_ntoa(srv); - - if (ret == 1) { - memset(list, ' ', 254); - list[255] = '\0'; - - if (server->name) { - memcpy(list, server->name, strlen(server->name)); - } else { - memcpy(list, "Unknown", 7); - } - - memcpy(list + 30, server->version, strlen(server->version)); - - unsigned a_len = strlen(address); - memcpy(list + 38, address, a_len); - memcpy(list + 38 + a_len, ":", 1); - - sprintf(buf, "%d", server->port); - - memcpy(list + 39 + a_len, buf, strlen(buf)); - - memcpy(list + 60, server->arena, strlen(server->arena)); - - sprintf(buf, "%d/%d", server->clients, server->maxclients); - - memcpy(list + 75, buf, strlen(buf)); - - sprintf(buf, "%dms", server->ping); - - memcpy(list + 83, buf, strlen(buf)); - - select_add(select_server, list); - } - } - - return 1; -} - -void browser_init() -{ - image_t *image; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), 100, WINDOW_SIZE_Y - 100, eventWidget); - button_play = button_new(_("Play"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, - eventWidget); - button_refresh = button_new(_("Refresh"), - WINDOW_SIZE_X / 2 - 130, WINDOW_SIZE_Y - 100, - eventWidget); - button_reload = button_new(_("Reload"), - WINDOW_SIZE_X / 2 + 35, WINDOW_SIZE_Y - 100, - eventWidget); - - label_server = label_new(_("Server"), 120, 145, WIDGET_LABEL_LEFT); - label_version = label_new(_("Version"), 290, 145, WIDGET_LABEL_LEFT); - label_address = label_new(_("IP"), 400, 145, WIDGET_LABEL_LEFT); - label_arena = label_new(_("Arena"), 550, 145, WIDGET_LABEL_LEFT); - label_players = label_new(_("Clients"), 645, 145, WIDGET_LABEL_LEFT); - label_ping = label_new(_("Ping"), 720, 145, WIDGET_LABEL_LEFT); - - select_server = select_new(50, label_server->y + 40, eventWidget); - - screen_register(screen_new("browser", browser_start, browser_event, - browser_draw, browser_stop)); - - server_list.next = &server_list; - server_list.prev = &server_list; -} - -void browser_quit() -{ - wid_image_destroy(image_backgorund); - - button_destroy(button_play); - button_destroy(button_back); - button_destroy(button_refresh); - button_destroy(button_reload); - - label_destroy(label_server); - label_destroy(label_version); - label_destroy(label_address); - label_destroy(label_arena); - label_destroy(label_players); - label_destroy(label_ping); - - select_destroy(select_server); -} - -static void clear_server_list() -{ - server_t *server; - - for (server = server_list.next; server != &server_list; server = server_list.next) { - server->next->prev = server->prev; - server->prev->next = server->next; - - if (server->name) { - free(server->name); - } - - if (server->version) { - free(server->version); - } - - if (server->arena) { - free(server->arena); - } - - free(server); - } -} diff --git a/src/screen/browser.h b/src/screen/browser.h deleted file mode 100644 index 76c0e42..0000000 --- a/src/screen/browser.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef SCREEN_BROWSER_H -#define SCREEN_BROWSER_H - -typedef struct server_context { - struct server_context *next, *prev; - - unsigned char state; - - unsigned char ping; - - unsigned ip; - unsigned port; - - char *name; - - char *version; - unsigned char clients; - unsigned char maxclients; - unsigned uptime; - char *arena; -} server_t; - -extern void browser_start(); -extern void browser_draw(); -extern void browser_event(); -extern void browser_stop(); -extern void browser_init(); -extern void browser_quit(); - -#endif /* SCREEN_BROWSER_H */ diff --git a/src/screen/choiceArena.c b/src/screen/choiceArena.c deleted file mode 100644 index d8c1a24..0000000 --- a/src/screen/choiceArena.c +++ /dev/null @@ -1,265 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "arenaFile.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" -#include "config.h" - -#ifndef NO_SOUND -#include "music.h" -#endif - -#include "mainMenu.h" -#include "choiceArena.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_buttonimage.h" - -static widget_t *image_backgorund; - -static widget_t *button_play; -static widget_t *button_back; -static widget_t *button_next; -static widget_t *button_prev; - -/*static list_t *listWidgetButtonimage;*/ /* map buttons to be showed and handled - well, show_map_buttons is used instead now */ -static list_t *listAllWidgetButtonimage; /* all map buttons */ -static int handled_map_button_group; /* 0 - first 6 buttons, 1 - second 6 buttons, ... */ -static void activateHandledMapButtonGroup(); -static struct show_map_buttons { - /* geez, it'd be nicer to use two lists - but how the hell list_t is supposed to be used?! :-) */ - int min; /* see activateHandledMapButtonGroup for description */ - int max; - int next; /* 0: max is last item+1 in listAllWidgetButtonimage, otherwise 1 */ - int prev; /* 0: min is first item in listAllWidgetButtonimage, otherwise 1 */ -} show_map_buttons = { 0,0,0,0 }; -static arenaFile_t *currentArena; - -static void hotkey_escape() -{ - screen_set("gameType"); -} - -void screen_startChoiceArena() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void choice_arena_draw() -{ - int i; - - wid_image_draw(image_backgorund); - - button_draw(button_play); - button_draw(button_back); - if (show_map_buttons.next) { button_draw(button_next); } - if (show_map_buttons.prev) { button_draw(button_prev); } - - for (i = show_map_buttons.min; i < show_map_buttons.max; i++) { - button_image_draw((widget_t *) listAllWidgetButtonimage->list[i]); - } -} - -void choice_arena_event() -{ - int i; - - button_event(button_play); - button_event(button_back); - if (show_map_buttons.next) { button_event(button_next); } - if (show_map_buttons.prev) { button_event(button_prev); } - - for (i = show_map_buttons.min ; i < show_map_buttons.max; i++) { - button_image_event((widget_t *) listAllWidgetButtonimage->list[i]); - } -} - -void stopScreenChoiceArena() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void button_eventImage(void *p) -{ - widget_t *buttonimage; - int i; - - buttonimage = (widget_t *) p; - - for (i = show_map_buttons.min; i < show_map_buttons.max; i++) { - widget_t *this; - - this = (widget_t *) listAllWidgetButtonimage->list[i]; - - if (buttonimage == this) { - button_image_set_active(this, TRUE); - currentArena = arena_file_get(i); - } else { - button_image_set_active(this, FALSE); - } - } -} - -arenaFile_t *choice_arena_get() -{ - return currentArena; -} - -void choice_arena_set(arenaFile_t *arenaFile) -{ - widget_t *widget_buttonimage; - int id; - - id = arena_file_get_id(arenaFile); - currentArena = arenaFile; - - if (id >= 0) { - widget_buttonimage = (widget_t *) listAllWidgetButtonimage->list[id]; - button_image_set_active(widget_buttonimage, TRUE); - } -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_play) { - screen_set("world"); - } - - if (button == button_back) { - screen_set("gameType"); - } - - if (button == button_next) { - if (show_map_buttons.next) { - handled_map_button_group++; - activateHandledMapButtonGroup(); - } - } - - if (button == button_prev) { - if (show_map_buttons.prev) { - handled_map_button_group--; - activateHandledMapButtonGroup(); - } - } -} - -/** - * groups of 6 map buttons to be shown and handled - * the group is set by static global variable handled_map_button_group - * sets static global show_map_buttons.min to index of first button to be drawn in listAllButtonimage - * and show_map_buttons.max to index of last button to be drawn+1 in listAllButtonimage - */ -static void activateHandledMapButtonGroup(void) -{ - int min = handled_map_button_group * 6; - min = min < 0 ? 0 : min; - min = min <= arena_file_get_count() ? min : arena_file_get_count(); - int max = min + 6 <= arena_file_get_count() ? min + 6 : arena_file_get_count(); - - if (min) { - show_map_buttons.prev = 1; - } else { - show_map_buttons.prev = 0; - } - - if (max < arena_file_get_count()) { - show_map_buttons.next = 1; - } else { - show_map_buttons.next = 0; - } - - show_map_buttons.min = min; - show_map_buttons.max = max; -} - -void choice_arena_init() -{ - image_t *image; - int i; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), 100, WINDOW_SIZE_Y - 100, eventWidget); - button_play = button_new(_("Play"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, eventWidget); - - button_prev = button_new(_("Previous arenas"), 250, WINDOW_SIZE_Y - 100, eventWidget); - button_next = button_new(_("Next arenas"), WINDOW_SIZE_X - 350, WINDOW_SIZE_Y - 100, eventWidget); - - listAllWidgetButtonimage = list_new(); - currentArena = NULL; - - for (i = 0; i < arena_file_get_count(); i++) { - widget_t *widget_buttonimage; - arenaFile_t *arenaFile; - int x, y; - - arenaFile = arena_file_get(i); - /*image = image_add(arena_file_get_image(i), IMAGE_NO_ALPHA, "none", IMAGE_GROUP_BASE);*/ - image = arena_file_load_image(arenaFile, arena_file_get_image(arenaFile), - IMAGE_GROUP_BASE, "none", IMAGE_NO_ALPHA); - - /* - only 6 map buttons can be shown at once; - set buttons' positions in each group to the corresponding values - handled_map_button_group and activateHandledMapButtonGroup is used - to choose group to be shown and handled - */ - int pos = i % 6 ; - x = 100 + 200 * (pos - 3 * (pos / 3)); - y = 150 + 200 * (pos / 3); - - widget_buttonimage = button_image_new(image, x, y, button_eventImage); -/* - if (i == choiceArenaId) { - widget_buttonimage->active = 1; - } -*/ - list_add(listAllWidgetButtonimage, widget_buttonimage); - handled_map_button_group = 0; - activateHandledMapButtonGroup(); - } - - screen_register(screen_new("chiceArena", screen_startChoiceArena, - choice_arena_event, choice_arena_draw, - stopScreenChoiceArena)); - - choice_arena_set(arena_file_get_file_format_net_name(config_get_str_value(CFG_ARENA))); -} - -void choice_arena_quit() -{ - if (choice_arena_get() != NULL) { - config_set_str_value(CFG_ARENA, arena_file_get_net_name(choice_arena_get())); - } - - wid_image_destroy(image_backgorund); - - button_destroy(button_play); - button_destroy(button_back); - - button_destroy(button_next); - button_destroy(button_prev); - - list_destroy_item(listAllWidgetButtonimage, button_image_destroy); -} diff --git a/src/screen/choiceArena.h b/src/screen/choiceArena.h deleted file mode 100644 index c0adacd..0000000 --- a/src/screen/choiceArena.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef SCREEN_CHOICE_ARENA_H -#define SCREEN_CHOICE_ARENA_H - -#include "main.h" -#include "arenaFile.h" - -#define SCREENSHOT_ARENA_WIDTH 160 -#define SCREENSHOT_ARENA_HEIGHT 107 - -extern void choice_arena_draw(); -extern void choice_arena_event(); -extern arenaFile_t *choice_arena_get(); -extern void choice_arena_set(arenaFile_t *arenaFile); -extern void choice_arena_init(); -extern void choice_arena_quit(); - -#endif /* SCREEN_CHOICE_ARENA_H */ diff --git a/src/screen/credits.c b/src/screen/credits.c deleted file mode 100644 index cb8704e..0000000 --- a/src/screen/credits.c +++ /dev/null @@ -1,162 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#endif - -#include "credits.h" - -#include "widget.h" -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" - -static widget_t *image_backgorund; -static widget_t *button_back; -static list_t *listWidgetLabel; -static textFile_t *textFile; -static int offset; -static int creditExists; - -static void hotkey_escape() -{ - screen_set("mainMenu"); -} - -void scredits_start() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - offset = 0; - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void scredits_draw() -{ - int i; - - wid_image_draw(image_backgorund); - - button_draw(button_back); - - for (i = 0; i < listWidgetLabel->count; i++) { - int z; - - widget_t *this; - - this = (widget_t *) listWidgetLabel->list[i]; - - z = this->y; - this->y += offset; - - if (this->y > SCREEN_CREDITS_OFFSET_MIN && this->y < SCREEN_CREDITS_OFFSET_MAX) { - label_draw(this); - } - - this->y = z; - } - -} - -void scredits_event() -{ - button_event(button_back); - - offset -= SCREEN_CREDITS_OFFSET_SPEED; - - if (offset < SCREEN_CREDITS_OFFSET_RESTART) { - offset = 0; - } - - /*printf("offset = %d\n", offset);*/ -} - -void scredits_stop() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_back) { - screen_set("mainMenu"); - } -} - -void scredits_init() -{ - image_t *image; - int i; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2, - WINDOW_SIZE_Y - 80, eventWidget); - - listWidgetLabel = list_new(); - - if (tryExistFile(PATH_DOC SCREEN_CREDITS_FILE) == 0) { - creditExists = 1; - textFile = text_file_load(PATH_DOC SCREEN_CREDITS_FILE); - - for (i = 0; i < textFile->text->count; i++) { - widget_t *label; - char *line; - - line = (char *) textFile->text->list[i]; - - label = label_new(line, WINDOW_SIZE_X / 3 - WINDOW_SIZE_X / 4, - (WINDOW_SIZE_Y - 100) + i * 20, - WIDGET_LABEL_LEFT); - - list_add(listWidgetLabel, label); - } - } else { - for (i = 0; i < 5; i++) { - creditExists = 0; - widget_t *label; - char line[STR_SIZE]; - - sprintf(line, _("[Error] Credits file not found [%s]: %s\n"), - PATH_DOC, SCREEN_CREDITS_FILE); - - label = label_new(line, WINDOW_SIZE_X / 2, - (WINDOW_SIZE_Y - 100) + i * 20, - WIDGET_LABEL_CENTER); - - list_add(listWidgetLabel, label); - } - } - - screen_register(screen_new("credits", scredits_start, scredits_event, - scredits_draw, scredits_stop)); -} - -void scredits_quit() -{ - wid_image_destroy(image_backgorund); - - button_destroy(button_back); - list_destroy_item(listWidgetLabel, label_destroy); - - if (creditExists) { - text_file_destroy(textFile); - } -} diff --git a/src/screen/credits.h b/src/screen/credits.h deleted file mode 100644 index 827b9c1..0000000 --- a/src/screen/credits.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef SCREEN_CREDITS_H -#define SCREEN_CREDITS_H - -#define SCREEN_CREDITS_FILE "AUTHORS" - -#define SCREEN_CREDITS_OFFSET_MIN 130 -#define SCREEN_CREDITS_OFFSET_MAX WINDOW_SIZE_Y-120 -#define SCREEN_CREDITS_OFFSET_RESTART -1000 -#define SCREEN_CREDITS_OFFSET_SPEED 2 - -extern void scredits_start(); -extern void scredits_draw(); -extern void scredits_event(); -extern void scredits_stop(); -extern void scredits_init(); -extern void scredits_quit(); - -#endif /* SCREEN_CREDITS_H */ diff --git a/src/screen/downArena.c b/src/screen/downArena.c deleted file mode 100644 index ac211a9..0000000 --- a/src/screen/downArena.c +++ /dev/null @@ -1,386 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "buffer.h" -#include "textFile.h" -#include "arenaFile.h" -#include "homeDirector.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" - -#include "net_multiplayer.h" -#include "udp.h" -#include "tcp.h" - -#include "widget.h" -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" - -#include "setting.h" -#include "downArena.h" -#include "gameType.h" - -static sock_udp_t *sock_server_udp; -static sock_tcp_t *sock_server_tcp; - -static widget_t *image_backgorund; -static widget_t *button_back; -static widget_t *label_status; - -static char str_status[STR_PROTO_SIZE]; -static char arenaNetName[STR_SIZE]; - -static int status_tcp_socket; -static buffer_t *recvBuffer; -static buffer_t *sendBuffer; - -static char path[STR_PATH_SIZE]; -static FILE *file; -static int fileSize; -static int fileOffset; - -static my_time_t timeSendMsg; -static int countSendMsg; - -static void setStatusString(char *s) -{ - printf("status -> %s\n", s); - label_destroy(label_status); - label_status = label_new(s, WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER); -} - -static void connectToDownServer() -{ - char status[STR_SIZE]; - char msg[STR_PROTO_SIZE]; - - sock_server_tcp = sock_tcp_connect(public_server_get_settingIP(), public_server_get_settingPort()); - - if (sock_server_tcp == NULL) { - sprintf(status, _("Error! Unable to connect to [%s]:%d download server"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); - return; - } - - sock_tcp_set_non_block(sock_server_tcp); - - recvBuffer = buffer_new(DOWN_ARENA_BUFFER_SIZE); - sendBuffer = buffer_new(DOWN_ARENA_BUFFER_SIZE); - status_tcp_socket = DOWN_SERVER_STATUS_OK; - - file = NULL; - countSendMsg = 0; - timeSendMsg = timer_get_current_time(); - - sprintf(msg, "GETARENA %s\n", arenaNetName); - - buffer_append(sendBuffer, msg, strlen(msg)); - - sprintf(status, _("Connecting to [%s]:%d download server"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); -} - -static void closeConnectFromDownServer() -{ - sock_tcp_close(sock_server_tcp); - buffer_destroy(recvBuffer); - buffer_destroy(sendBuffer); - sock_server_tcp = NULL; - recvBuffer = NULL; - sendBuffer = NULL; -} - -static void sendStatusToGameServer() -{ - char *msg = "status\n"; - - if (countSendMsg > DOWN_ARENA_MAX_SEND_MSG_STATUS) { - char status[STR_SIZE]; - sprintf(status, _("Error! Game server [%s]:%d is off"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); - return; - } - - timeSendMsg = timer_get_current_time(); - countSendMsg++; - printf("countSendMsg = %d\n", countSendMsg++); - - if (sock_udp_write(sock_server_udp, sock_server_udp, msg, strlen(msg)) < 0) { - char status[STR_SIZE]; - sprintf(status, _("Error! Unable to send message to [%s]:%d game server"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); - } -} - -void down_arena_start() -{ - char status[STR_SIZE]; - - strcpy(str_status, "none"); - memset(arenaNetName, 0, STR_SIZE); - - sock_server_udp = sock_udp_connect(public_server_get_settingIP(), public_server_get_settingPort()); - - if (sock_server_udp == NULL) { - sprintf(status, _("Error! Unable to connect to [%s]:%d game server"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); - return; - } - - sock_udp_set_non_block(sock_server_udp); - - sprintf(status, _("Connecting to [%s]:%d game server"), - public_server_get_settingIP(), public_server_get_settingPort()); - setStatusString(status); - - sendStatusToGameServer(); -} - -void down_arena_draw() -{ - wid_image_draw(image_backgorund); - label_draw(label_status); - button_draw(button_back); -} - -static void proto_ok(char *line) -{ - /*printf("line = %s\n", line);*/ - fileSize = atoi(line + 3); - fileOffset = 0; - - sprintf(path, "%s/%s.zip", home_director_get(), arenaNetName); - /*printf("path = %s\n", path);*/ - file = fopen(path, "wb"); -} - -static void proto_err(char *line) -{ - /*printf("line = %s\n", line);*/ - setStatusString(line + 4); - closeConnectFromDownServer(); -} - -static void eventProto(char *line) -{ - if (strncmp(line, "OK", 2) == 0) { - proto_ok(line); - } - - if (strncmp(line, "ERR", 3) == 0) { - proto_err(line); - } -} - -static int downArenaFile() -{ - char status[STR_SIZE]; - void *data; - int len; - - len = buffer_get_size(recvBuffer); - data = buffer_get_data(recvBuffer); - buffer_cut(recvBuffer, len); - - fwrite(data, len, 1, file); - fileOffset += len; - - sprintf(status, _("Downloading: %d/%d"), fileOffset, fileSize); - setStatusString(status); - - return len; -} - -static int eventRecvBuffer() -{ - char buffer[STR_PROTO_SIZE]; - char line[STR_PROTO_SIZE]; - int ret; - - memset(buffer, 0, STR_PROTO_SIZE); - - ret = sock_tcp_read(sock_server_tcp, buffer, STR_PROTO_SIZE - 1); - /*if (ret <= 0) printf("sock_tcp_read = %d\n", ret);*/ - - if (ret < 0) { - return -1; - } - - if (buffer_append(recvBuffer, buffer, ret) < 0) { - char status[STR_SIZE]; - sprintf(status, _("Error! Receiving buffer is full")); - setStatusString(status); - closeConnectFromDownServer(); - return -1; - } - - if (file == NULL && buffer_get_line(recvBuffer, line, STR_PROTO_SIZE) >= 0) { - eventProto(line); - return ret; - } - - if (file != NULL) { - return downArenaFile(); - } - - return -1; -} - -static int eventSendBuffer() -{ - void *data; - int len; - int res; - - len = buffer_get_size(sendBuffer); - - if (len == 0) { - return -1; - } - - data = buffer_get_data(sendBuffer); - - res = sock_tcp_write(sock_server_tcp, data, len); - - if (res < 0) { - return -1; - } - - buffer_cut(sendBuffer, res); - return res; -} - -static void readArenaFromStatus() -{ - const char *str_arena = "arena: "; - - char *offset_begin; - char *offset_end; - - offset_begin = strstr(str_status, str_arena); - - if (offset_begin != NULL) { - offset_begin += strlen(str_arena); - offset_end = strstr(offset_begin, "\n"); - - if (offset_end != NULL) { - sock_udp_close(sock_server_udp); - sock_server_udp = NULL; - strncpy(arenaNetName, offset_begin, offset_end - offset_begin); - - if (arena_file_get_file_format_net_name(arenaNetName) != NULL) { - screen_set("world"); - return; - } - - connectToDownServer(); - - debug("arenaNetName: %s", arenaNetName); - } - } -} - -static void eventStatus() -{ - if (timer_get_current_time() - timeSendMsg > DOWN_ARENA_MAX_TIEMOUT_LIMIT) { - sendStatusToGameServer(); - } - - if (sock_udp_read(sock_server_udp, sock_server_udp, str_status, STR_PROTO_SIZE - 1) > 0) { - /*printf("str_status = %s\n", str_status);*/ - readArenaFromStatus(); - } -} - -void down_arena_event() -{ - int i; - - if (strcmp(str_status, "none") == 0) { - eventStatus(); - } - - if (recvBuffer != NULL) { - for (i = 0; i < DOWN_ARENA_COUNT_READ_SOCKET; i++) { - if (eventRecvBuffer() <= 0) { - break; - } - } - } - - if (sendBuffer != NULL) { - eventSendBuffer(); - } - - if (file != NULL && fileSize == fileOffset && fileSize > 0) { - fclose(file); - file = NULL; - closeConnectFromDownServer(); - arena_file_load(path); - screen_set("world"); - } - - button_event(button_back); -} - -void down_arena_stop() -{ - if (file != NULL) { - unlink(path); - } - - if (sock_server_tcp != NULL) { - closeConnectFromDownServer(); - } - - if (sock_server_udp != NULL) { - sock_udp_close(sock_server_udp); - } - -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_back) { - screen_set("mainMenu"); - } -} - -void down_arena_init() -{ - image_t *image; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2, - WINDOW_SIZE_Y - 80, eventWidget); - - label_status = label_new(_("none"), WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER); - - screen_register(screen_new("downArena", down_arena_start, down_arena_event, - down_arena_draw, down_arena_stop)); -} - -void down_arena_quit() -{ - wid_image_destroy(image_backgorund); - button_destroy(button_back); - label_destroy(label_status); -} diff --git a/src/screen/downArena.h b/src/screen/downArena.h deleted file mode 100644 index 444210c..0000000 --- a/src/screen/downArena.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef SCREEN_DOWN_ARENA_H -#define SCREEN_DOWN_ARENA_H - -#define DOWN_SERVER_STATUS_OK 0 -#define DOWN_SERVER_STATUS_ZOMBIE 1 - -#define DOWN_ARENA_BUFFER_SIZE 4096 - -#define DOWN_ARENA_MAX_SEND_MSG_STATUS 10 -#define DOWN_ARENA_MAX_TIEMOUT_LIMIT 500 -#define DOWN_ARENA_COUNT_READ_SOCKET 100 - -extern void down_arena_start(); -extern void down_arena_draw(); -extern void down_arena_event(); -extern void down_arena_stop(); -extern void down_arena_init(); -extern void down_arena_quit(); - -#endif /* SCREEN_DOWN_ARENA_H */ diff --git a/src/screen/gameType.c b/src/screen/gameType.c deleted file mode 100644 index e173ab2..0000000 --- a/src/screen/gameType.c +++ /dev/null @@ -1,416 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "director.h" -#include "homeDirector.h" -#include "net_multiplayer.h" -#include "tux.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" -#include "config.h" - -#ifndef NO_SOUND -#include "music.h" -#endif /* NO_SOUND */ - -#include "gameType.h" -#include "setting.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_textfield.h" -#include "widget_choicegroup.h" -#include "widget_check.h" -#include "widget_select.h" - -static widget_t *image_backgorund; - -static widget_t *button_back; -static widget_t *button_play; -static widget_t *button_browser; - -static widget_t *label_none; -static widget_t *label_server; -static widget_t *label_client; -static widget_t *label_load_session; - -static widget_t *label_ip; -static widget_t *label_port; -static widget_t *label_session; - -static list_t *listChoiceGroup; -static widget_t *check_none; -static widget_t *check_server; -static widget_t *check_client; -static widget_t *check_load_session; - -static widget_t *textfield_ip; -static widget_t *textfield_port; - -static widget_t *selectSession; - -static void loadSession(widget_t *p) -{ - director_t *director; - int i; - - director = director_load(home_director_get()); - - select_remove_all(p); - - if (director == NULL) { - return; - } - - for (i = 0; i < director->list->count; i++) { - char *line; - - line = director->list->list[i]; - - if (strstr(line, ".sav") != NULL) { - select_add(p, line); - } - } - - director_destroy(director); -} - -static void hotkey_escape() -{ - screen_set("mainMenu"); -} - -void screen_startGameType() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif /* NO_SOUND */ - -/* - choiceGroup_set_status(check_none, TRUE); - choiceGroup_set_status(check_server, FALSE); - choiceGroup_set_status(check_client, FALSE); - choiceGroup_set_status(check_load_session, FALSE); -*/ - - loadSession(selectSession); - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void game_type_draw() -{ - int i; - - wid_image_draw(image_backgorund); - - for (i = 0; i < listChoiceGroup->count; i++) { - widget_t *this; - - this = (widget_t *) listChoiceGroup->list[i]; - choice_group_draw(this); - } - - label_draw(label_none); - label_draw(label_server); - label_draw(label_client); - label_draw(label_load_session); - - if (choice_group_get_status(check_server) == TRUE || choice_group_get_status(check_client) == TRUE) { - label_draw(label_port); - text_field_draw(textfield_port); - label_draw(label_ip); - text_field_draw(textfield_ip); - } - - if (choice_group_get_status(check_load_session) == TRUE) { - label_draw(label_session); - select_draw(selectSession); - } -/* - if (check_server->status == TRUE) { - game_type_set_ip(getParamElse("--ip", "127.0.0.1")); - game_type_set_port(atoi(getParamElse("--port", "2200"))); - } -*/ - - if (choice_group_get_status(check_client) == TRUE) { - button_draw(button_browser); - } - - button_draw(button_back); - button_draw(button_play); -} - -void game_type_event() -{ - int i; - - for (i = 0; i < listChoiceGroup->count; i++) { - widget_t *this; - - this = (widget_t *) listChoiceGroup->list[i]; - choice_group_event(this); - } - - if (choice_group_get_status(check_server) == TRUE || choice_group_get_status(check_client) == TRUE) { - text_field_event(textfield_ip); - text_field_event(textfield_port); - } - - if (choice_group_get_status(check_load_session) == TRUE) { - select_event(selectSession); - } - - button_event(button_back); - button_event(button_play); - - if (choice_group_get_status(check_client) == TRUE) { - button_event(button_browser); - } -} - -void stopScreenGameType() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_back) { - screen_set("mainMenu"); - return; - } - - if (button == button_play) { - char *str; - - str = game_type_load_session(); - - if (str != NULL) { - screen_set("world"); - return; - } - - if (public_server_get_settingGameType() == NET_GAME_TYPE_CLIENT) { - screen_set("downArena"); - } else { - screen_set("chiceArena"); - } - - return; - } - - if (button == button_browser) { - screen_set("browser"); - return; - } -} - -void game_type_init() -{ - image_t *image; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), 100, WINDOW_SIZE_Y - 100, eventWidget); - button_play = button_new(_("Play"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, eventWidget); - button_browser = button_new(_("Browser"), 300, 345, eventWidget); - - listChoiceGroup = list_new(); - check_none = choice_group_new(100, 150, FALSE, listChoiceGroup, eventWidget); - check_server = choice_group_new(100, 200, FALSE, listChoiceGroup, eventWidget); - check_client = choice_group_new(100, 250, FALSE, listChoiceGroup, eventWidget); - check_load_session = choice_group_new(100, 300, FALSE, listChoiceGroup, eventWidget); - - if (strcmp(config_get_str_value(CFG_GAME_TYPE), "server") == 0) { - choiceGroup_set_status(check_server, TRUE); - } else if (strcmp(config_get_str_value(CFG_GAME_TYPE), "client") == 0) { - choiceGroup_set_status(check_client, TRUE); - } else if (strcmp(config_get_str_value(CFG_GAME_TYPE), "none") == 0) { - choiceGroup_set_status(check_none, TRUE); - } else { - choiceGroup_set_status(check_load_session, TRUE); - } - - label_none = label_new(_("Local game"), 130, 145, WIDGET_LABEL_LEFT); - label_server = label_new(_("Set up a server"), 130, 195, WIDGET_LABEL_LEFT); - label_client = label_new(_("Network game"), 130, 245, WIDGET_LABEL_LEFT); - label_load_session = label_new(_("Load saved game"), 130, 295, WIDGET_LABEL_LEFT); - - label_ip = label_new(_("IP"), 300, 145, WIDGET_LABEL_LEFT); - label_port = label_new(_("Port"), 300, 245, WIDGET_LABEL_LEFT); - label_session = label_new(_("Load saved game:"), 300, 145, WIDGET_LABEL_LEFT); - - textfield_ip = text_field_new(config_get_str_value(CFG_NET_IP), - WIDGET_TEXTFIELD_FILTER_IP_OR_DOMAIN, - 300, 180); - - textfield_port = text_field_new(config_get_str_value(CFG_NET_PORT), - WIDGET_TEXTFIELD_FILTER_NUM, 300, 280); - - selectSession = select_new(300, 185, eventWidget); - - screen_register(screen_new("gameType", screen_startGameType, game_type_event, - game_type_draw, stopScreenGameType)); -} - -int setSettingGameType(int status) -{ - if (status == NET_GAME_TYPE_NONE) { - choiceGroup_set_status(check_none, TRUE); - return 0; - } - - if (status == NET_GAME_TYPE_SERVER) { - choiceGroup_set_status(check_server, TRUE); - return 0; - } - - if (status == NET_GAME_TYPE_CLIENT) { - choiceGroup_set_status(check_client, TRUE); - return 0; - } - - return -1; -} - -int public_server_get_settingGameType() -{ - if (choice_group_get_status(check_none) == TRUE || choice_group_get_status(check_load_session) == TRUE) { - return NET_GAME_TYPE_NONE; - } - - if (choice_group_get_status(check_server) == TRUE) { - return NET_GAME_TYPE_SERVER; - } - - if (choice_group_get_status(check_client) == TRUE) { - return NET_GAME_TYPE_CLIENT; - } - - fatal("Unknown game type"); - - return -1; -} - -char *game_type_load_session() -{ - if (choice_group_get_status(check_load_session) == TRUE) { - return select_get_item(selectSession); - } - - return NULL; -} - -int game_type_set_ip(char *address) -{ - char str[STR_SIZE]; - - strcpy(str, address); - text_field_set_text(textfield_ip, str); - - return 1; -} - -char *public_server_get_settingIP() -{ - return text_field_get_text(textfield_ip); -} - -int game_type_set_port(int port) -{ - char str[STR_SIZE]; - - sprintf(str, "%d", port); - text_field_set_text(textfield_port, str); - - return 0; -} - -int public_server_get_settingPort() -{ - return atoi(text_field_get_text(textfield_port)); -} - -int public_server_get_settingProto() -{ - if (strstr(text_field_get_text(textfield_ip), ".") != NULL) { - return PROTO_UDPv4; - } - - if (strstr(text_field_get_text(textfield_ip), ":") != NULL) { - return PROTO_UDPv6; - } - - debug("Unknown version of IP protocol"); - - return -1; -} - -static void save_config() -{ - if (choice_group_get_status(check_none) == TRUE) { - config_set_str_value(CFG_GAME_TYPE, "none"); - } - - if (choice_group_get_status(check_server) == TRUE) { - config_set_str_value(CFG_GAME_TYPE, "server"); - } - - if (choice_group_get_status(check_client) == TRUE) { - config_set_str_value(CFG_GAME_TYPE, "client"); - } - - if (choice_group_get_status(check_load_session) == TRUE) { - config_set_str_value(CFG_GAME_TYPE, "session"); - } - - config_set_str_value(CFG_NET_IP, text_field_get_text(textfield_ip)); - config_set_str_value(CFG_NET_PORT, text_field_get_text(textfield_port)); -} - -void game_type_quit() -{ - save_config(); - - wid_image_destroy(image_backgorund); - - label_destroy(label_none); - label_destroy(label_server); - label_destroy(label_client); - label_destroy(label_load_session); - - label_destroy(label_ip); - label_destroy(label_port); - label_destroy(label_session); - - text_field_destroy(textfield_ip); - text_field_destroy(textfield_port); - - select_destroy(selectSession); - - button_destroy(button_back); - button_destroy(button_play); - button_destroy(button_browser); - - choice_group_destroy(check_none); - choice_group_destroy(check_server); - choice_group_destroy(check_client); - choice_group_destroy(check_load_session); - - list_destroy(listChoiceGroup); -} diff --git a/src/screen/gameType.h b/src/screen/gameType.h deleted file mode 100644 index 49cf008..0000000 --- a/src/screen/gameType.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SCREEN_GAME_TYPE_H -#define SCREEN_GAME_TYPE_H - -#include "main.h" - -extern void game_type_draw(); -extern void game_type_event(); -extern void game_type_init(); -extern int setSettingGameType(int status); -extern int public_server_get_settingGameType(); -extern char *game_type_load_session(); -extern int game_type_set_ip(char *address); -extern char *public_server_get_settingIP(); -extern int game_type_set_port(int port); -extern int public_server_get_settingPort(); -extern int public_server_get_settingProto(); -extern void game_type_quit(); - -#endif /* SCREEN_GAME_TYPE_H */ diff --git a/src/screen/mainMenu.c b/src/screen/mainMenu.c deleted file mode 100644 index 147ef59..0000000 --- a/src/screen/mainMenu.c +++ /dev/null @@ -1,149 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include "main.h" - -#include "game.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#endif - -#include "mainMenu.h" -#include "setting.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_textfield.h" -#include "widget_check.h" -#include "widget_image.h" -#include "widget_container.h" - -static widget_t *image_backgorund; - -static widget_t *widget_container; -static widget_t *button_play; -static widget_t *button_setting; -static widget_t *button_table; -static widget_t *button_credits; -static widget_t *button_end; - -static void hotkey_escape() -{ - game_quit(); -} - -void main_menu_start() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void main_menu_draw() -{ - wid_image_draw(image_backgorund); - - widget_container_draw(widget_container); -/* - button_draw(button_play); - button_draw(button_setting); - button_draw(button_table); - button_draw(button_credits); - button_draw(button_end); -*/ -} - -void main_menu_event() -{ - widget_container_event(widget_container); -/* - button_event(button_play); - button_event(button_setting); - button_event(button_table); - button_event(button_credits); - button_event(button_end); -*/ -} - -void main_menu_stop() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_play) { - screen_set("gameType"); - } - - if (button == button_setting) { - screen_set("setting"); - } - - if (button == button_table) { - screen_set("table"); - } - - if (button == button_credits) { - screen_set("credits"); - } - - if (button == button_end) { - game_quit(); - } -} - -void main_menu_init() -{ - image_t *image; - - image = image_add("screen_main.png", IMAGE_NO_ALPHA, - "screen_main", IMAGE_GROUP_BASE); - image_backgorund = wid_image_new(0, 0, image); - - widget_container = widget_container_new(WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2, 200, WIDGET_BUTTON_WIDTH, 300); - - button_play = button_new(_("Start game"), WIDGET_NONE_VALUE, WIDGET_NONE_VALUE, eventWidget); - button_setting = button_new(_("Settings"), WIDGET_NONE_VALUE, WIDGET_NONE_VALUE, eventWidget); - button_table = button_new(_("Highscore"), WIDGET_NONE_VALUE, WIDGET_NONE_VALUE, eventWidget); - button_credits = button_new(_("Credits"), WIDGET_NONE_VALUE, WIDGET_NONE_VALUE, eventWidget); - button_end = button_new(_("Quit game"), WIDGET_NONE_VALUE, WIDGET_NONE_VALUE, eventWidget); - - widget_container_add(widget_container, button_play); - widget_container_add(widget_container, button_setting); - widget_container_add(widget_container, button_table); - widget_container_add(widget_container, button_credits); - widget_container_add(widget_container, button_end); - - widget_set_layout_table(widget_container, 1, 5, 0, 50); - -#ifndef NO_SOUND - music_add("menu.ogg", "menu", MUSIC_GROUP_BASE); -#endif - - screen_register(screen_new("mainMenu", main_menu_start, main_menu_event, - main_menu_draw, main_menu_stop)); -} - -void main_menu_quit() -{ - wid_image_destroy(image_backgorund); - - button_destroy(button_play); - button_destroy(button_setting); - button_destroy(button_table); - button_destroy(button_credits); - button_destroy(button_end); -} diff --git a/src/screen/mainMenu.h b/src/screen/mainMenu.h deleted file mode 100644 index 99249b4..0000000 --- a/src/screen/mainMenu.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SCREEN_MAIN_MENU_H -#define SCREEN_MAIN_MENU_H - -#include "main.h" - -extern void main_menu_start(); -extern void main_menu_draw(); -extern void main_menu_event(); -extern void main_menu_stop(); -extern void main_menu_init(); -extern void main_menu_quit(); - -#endif /* SCREEN_MAIN_MENU_H */ diff --git a/src/screen/setting.c b/src/screen/setting.c deleted file mode 100644 index 2e1b615..0000000 --- a/src/screen/setting.c +++ /dev/null @@ -1,511 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "textFile.h" -#include "director.h" -#include "homeDirector.h" -#include "arenaFile.h" -#include "config.h" - -#include "configFile.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#include "sound.h" -#endif - -#include "mainMenu.h" -#include "setting.h" -#include "settingKeys.h" -#include "settingKeys.h" -#include "choiceArena.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_textfield.h" -#include "widget_check.h" -#include "widget_select.h" -#include "widget_statusbar.h" - -static widget_t *image_backgorund; - -static widget_t *button_back; -static widget_t *button_keys; /* setting of keycontrols */ - -static widget_t *label_count_round; -static widget_t *label_name_player1; -static widget_t *label_name_player2; -static widget_t *label_ai; - -#ifndef NO_SOUND -static widget_t *label_music; -static widget_t *label_sound; -#endif - -static widget_t *label_weapons; -static widget_t *label_bonuses; - -static widget_t *check[ITEM_COUNT]; -#ifndef NO_SOUND -static widget_t *check_music; -static widget_t *check_sound; -#endif - -static widget_t *image_gun_dual_revolver; -static widget_t *image_gun_scatter; -static widget_t *image_gun_tommy; -static widget_t *image_gun_lasser; -static widget_t *image_gun_mine; -static widget_t *image_gun_bombball; - -static widget_t *image_bonus_speed; -static widget_t *image_bonus_shot; -static widget_t *image_bonus_teleport; -static widget_t *image_bonus_ghost; -static widget_t *image_bonus_4x; -static widget_t *image_bonus_hidden; - -static widget_t *textfield_count_cound; -static widget_t *textfield_name_player1; -static widget_t *textfield_name_player2; - -static widget_t *check_ai; - -static widget_t *statusbar; - -static void hotkey_escape() -{ - screen_set("mainMenu"); -} - -void screen_startSetting() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void setting_draw() -{ - int i; - - wid_image_draw(image_backgorund); - - label_draw(label_weapons); - label_draw(label_bonuses); - - label_draw(label_count_round); - label_draw(label_name_player1); - label_draw(label_ai); - -#ifndef NO_SOUND - label_draw(label_music); - label_draw(label_sound); -#endif - - text_field_draw(textfield_count_cound); - text_field_draw(textfield_name_player1); - - if (check_get_status(check_ai) == FALSE) { - label_draw(label_name_player2); - text_field_draw(textfield_name_player2); - } - - wid_image_draw(image_gun_dual_revolver); - wid_image_draw(image_gun_scatter); - wid_image_draw(image_gun_tommy); - wid_image_draw(image_gun_lasser); - wid_image_draw(image_gun_mine); - wid_image_draw(image_gun_bombball); - - wid_image_draw(image_bonus_speed); - wid_image_draw(image_bonus_shot); - wid_image_draw(image_bonus_teleport); - wid_image_draw(image_bonus_ghost); - wid_image_draw(image_bonus_4x); - wid_image_draw(image_bonus_hidden); - -#ifndef NO_SOUND - check_draw(check_music); - check_draw(check_sound); -#endif - - for (i = GUN_DUAL_SIMPLE; i <= GUN_BOMBBALL; i++) { - check_draw(check[i]); - } - - for (i = BONUS_SPEED; i <= BONUS_HIDDEN; i++) { - check_draw(check[i]); - } - - check_draw(check_ai); - - statusbar_draw(statusbar); - - button_draw(button_back); - button_draw(button_keys); -} - -void setting_event() -{ - int i; - - text_field_event(textfield_count_cound); - text_field_event(textfield_name_player1); - - if (check_get_status(check_ai) == FALSE) { - text_field_event(textfield_name_player2); - } - -#ifndef NO_SOUND - check_event(check_music); - check_event(check_sound); -#endif - - for (i = GUN_DUAL_SIMPLE; i <= GUN_BOMBBALL; i++) { - check_event(check[i]); - } - - for (i = BONUS_SPEED; i <= BONUS_HIDDEN; i++) { - check_event(check[i]); - } - - check_event(check_ai); - - statusbar_event(statusbar); - - button_event(button_back); - button_event(button_keys); -} - -void stopScreenSetting() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *my_button; - widget_t *my_check; - - my_button = (widget_t *) p; - my_check = (widget_t *) p; - - if (my_button == button_back) { - screen_set("mainMenu"); - } - - if (my_button == button_keys) { - screen_set("settingKeys"); - } - -#ifndef NO_SOUND - if (my_check == check_music) { - music_set_active(check_get_status(check_music)); - } - - if (my_check == check_sound) { - sound_set_active(check_get_status(check_sound)); - } -#endif -} - -static void initSettingFile() -{ - text_field_set_text(textfield_count_cound, get_string_static(config_get_int_value(CFG_COUNT_ROUND))); - text_field_set_text(textfield_name_player1, config_get_str_value(CFG_NAME_RIGHT)); - text_field_set_text(textfield_name_player2, config_get_str_value(CFG_NAME_LEFT)); - - check_set_status(check[GUN_DUAL_SIMPLE], config_get_int_value(CFG_GUN_DUAL_SIMPLE)); - check_set_status(check[GUN_SCATTER], config_get_int_value(CFG_GUN_SCATTER)); - check_set_status(check[GUN_TOMMY], config_get_int_value(CFG_GUN_TOMMY)); - check_set_status(check[GUN_LASSER], config_get_int_value(CFG_GUN_LASSER)); - check_set_status(check[GUN_MINE], config_get_int_value(CFG_GUN_MINE)); - check_set_status(check[GUN_BOMBBALL], config_get_int_value(CFG_GUN_BOMBBALL)); - - check_set_status(check[BONUS_SPEED], config_get_int_value(CFG_BONUS_SPEED)); - check_set_status(check[BONUS_SHOT], config_get_int_value(CFG_BONUS_SHOT)); - check_set_status(check[BONUS_TELEPORT], config_get_int_value(CFG_BONUS_TELEPORT)); - check_set_status(check[BONUS_GHOST], config_get_int_value(CFG_BONUS_GHOST)); - check_set_status(check[BONUS_4X], config_get_int_value(CFG_BONUS_4X)); - check_set_status(check[BONUS_HIDDEN], config_get_int_value(CFG_BONUS_HIDDEN)); - - check_set_status(check_ai, config_get_int_value(CFG_AI)); - -#ifndef NO_SOUND - check_set_status(check_music, config_get_int_value(CFG_MUSIC)); - check_set_status(check_sound, config_get_int_value(CFG_SOUND)); -#endif -} - -static void saveAndDestroyConfigFile() -{ - config_set_int_value(CFG_COUNT_ROUND, atoi(text_field_get_text(textfield_count_cound))); - config_set_str_value(CFG_NAME_RIGHT, text_field_get_text(textfield_name_player1)); - config_set_str_value(CFG_NAME_LEFT, text_field_get_text(textfield_name_player2)); - - config_set_int_value(CFG_GUN_DUAL_SIMPLE, check_get_status(check[GUN_DUAL_SIMPLE])); - config_set_int_value(CFG_GUN_SCATTER, check_get_status(check[GUN_SCATTER])); - config_set_int_value(CFG_GUN_TOMMY, check_get_status(check[GUN_TOMMY])); - config_set_int_value(CFG_GUN_LASSER, check_get_status(check[GUN_LASSER])); - config_set_int_value(CFG_GUN_MINE, check_get_status(check[GUN_MINE])); - config_set_int_value(CFG_GUN_BOMBBALL, check_get_status(check[GUN_BOMBBALL])); - config_set_int_value(CFG_BONUS_SPEED, check_get_status(check[BONUS_SPEED])); - config_set_int_value(CFG_BONUS_SHOT, check_get_status(check[BONUS_SHOT])); - config_set_int_value(CFG_BONUS_TELEPORT, check_get_status(check[BONUS_TELEPORT])); - config_set_int_value(CFG_BONUS_GHOST, check_get_status(check[BONUS_GHOST])); - config_set_int_value(CFG_BONUS_4X, check_get_status(check[BONUS_4X])); - config_set_int_value(CFG_BONUS_HIDDEN, check_get_status(check[BONUS_HIDDEN])); - - config_set_int_value(CFG_AI, check_get_status(check_ai)); - -#ifndef NO_SOUND - config_set_int_value(CFG_MUSIC, check_get_status(check_music)); - config_set_int_value(CFG_SOUND, check_get_status(check_sound)); -#endif -} - -void setting_init() -{ - image_t *image; - int i; - - image = image_get(IMAGE_GROUP_BASE, "screen_main"); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, eventWidget); - - button_keys = button_new(_("Controls"), WINDOW_SIZE_X - 200, - button_back->y - WIDGET_BUTTON_HEIGHT - 10, eventWidget); - -#ifndef NO_SOUND - label_music = label_new(_("Music:"), 100, WINDOW_SIZE_Y - 85, WIDGET_LABEL_LEFT); - - check_music = check_new(label_music->x + label_music->w + 10, WINDOW_SIZE_Y - 80, - music_is_active(), eventWidget); - label_sound = label_new(_("Sound:"), check_music->x + WIDGET_CHECK_WIDTH + 10, - WINDOW_SIZE_Y - 85, WIDGET_LABEL_LEFT); - - check_sound = check_new(label_sound->x + label_sound->w + 10, WINDOW_SIZE_Y - 80, - sound_is_active(), eventWidget); - - label_ai = label_new(_("AI:"), check_sound->x + WIDGET_CHECK_WIDTH + 10, - WINDOW_SIZE_Y - 85, WIDGET_LABEL_LEFT); -#else - label_ai = label_new(_("AI:"), 100, WINDOW_SIZE_Y - 85, WIDGET_LABEL_LEFT); -#endif - - check_ai = check_new(label_ai->x + label_ai->w + 10, WINDOW_SIZE_Y - 80, FALSE, eventWidget); - - label_count_round = label_new(_("No. of rounds:"), 100, WINDOW_SIZE_Y - 200, WIDGET_LABEL_LEFT); - label_name_player1 = label_new(_("Player 1:"), 100, WINDOW_SIZE_Y - 160, WIDGET_LABEL_LEFT); - label_name_player2 = label_new(_("Player 2:"), 100, WINDOW_SIZE_Y - 120, WIDGET_LABEL_LEFT); - - textfield_count_cound = text_field_new(getParamElse("--count", "15"), - WIDGET_TEXTFIELD_FILTER_NUM, - 110 + label_count_round->w, - WINDOW_SIZE_Y - 200); - - textfield_name_player1 = text_field_new(getParamElse("--name1", NAME_PLAYER_RIGHT), - WIDGET_TEXTFIELD_FILTER_ALPHANUM, - 110 + label_count_round->w, WINDOW_SIZE_Y - 160); - - textfield_name_player2 = text_field_new(getParamElse("--name2", NAME_PLAYER_LEFT), - WIDGET_TEXTFIELD_FILTER_ALPHANUM, - 110 + label_count_round->w, WINDOW_SIZE_Y - 120); - - label_weapons = label_new(_("Weapons:"), 100, 160, WIDGET_LABEL_LEFT); - label_bonuses = label_new(_("Bonuses:"), 430, 160, WIDGET_LABEL_LEFT); - - for (i = GUN_DUAL_SIMPLE; i <= GUN_BOMBBALL; i++) { - int x = 0; - int y = 0; - - if (i >= GUN_DUAL_SIMPLE && i <= GUN_TOMMY) { - x = 100; - y = 200 + (i - GUN_DUAL_SIMPLE) * 50; - } - - if (i >= GUN_LASSER && i <= GUN_BOMBBALL) { - x = 250; - y = 200 + (i - GUN_LASSER) * 50; - } - - check[i] = check_new(x, y, TRUE, NULL); - } - - for (i = BONUS_SPEED; i <= BONUS_HIDDEN; i++) { - int x = 0; - int y = 0; - - if (i >= BONUS_SPEED && i <= BONUS_TELEPORT) { - x = 430; - y = 200 + (i - BONUS_SPEED) * 50; - } - - if (i >= BONUS_GHOST && i <= BONUS_HIDDEN) { - x = 580; - y = 200 + (i - BONUS_GHOST) * 50; - } - - check[i] = check_new(x, y, TRUE, NULL); - } - - image_gun_dual_revolver = wid_image_new(110 + WIDGET_CHECK_WIDTH, 200, image_get(IMAGE_GROUP_BASE, "panel_dual")); - image_gun_scatter = wid_image_new(110 + WIDGET_CHECK_WIDTH, 250, image_get(IMAGE_GROUP_BASE, "panel_scatter")); - image_gun_tommy = wid_image_new(110 + WIDGET_CHECK_WIDTH, 300, image_get(IMAGE_GROUP_BASE, "panel_tommy")); - image_gun_lasser = wid_image_new(260 + WIDGET_CHECK_WIDTH, 200,image_get(IMAGE_GROUP_BASE, "panel_lasser")); - image_gun_mine = wid_image_new(260 + WIDGET_CHECK_WIDTH, 250, image_get(IMAGE_GROUP_BASE, "panel_mine")); - image_gun_bombball = wid_image_new(260 + WIDGET_CHECK_WIDTH, 300, image_get(IMAGE_GROUP_BASE, "panel_bombball")); - image_bonus_speed = wid_image_new(430 + WIDGET_CHECK_WIDTH, 200, image_get(IMAGE_GROUP_BASE, "panel_speed"));; - image_bonus_shot = wid_image_new(430 + WIDGET_CHECK_WIDTH, 250, image_get(IMAGE_GROUP_BASE, "panel_shot"));; - image_bonus_teleport = wid_image_new(430 + WIDGET_CHECK_WIDTH, 300, image_get(IMAGE_GROUP_BASE, "panel_teleport"));; - image_bonus_ghost = wid_image_new(580 + WIDGET_CHECK_WIDTH, 200, image_get(IMAGE_GROUP_BASE, "panel_ghost"));; - image_bonus_4x = wid_image_new(580 + WIDGET_CHECK_WIDTH, 250, image_get(IMAGE_GROUP_BASE, "panel_4x"));; - image_bonus_hidden = wid_image_new(580 + WIDGET_CHECK_WIDTH, 300, image_get(IMAGE_GROUP_BASE, "panel_hidden"));; - - statusbar = statusbar_new(400, 350); - statusbar_add(statusbar, check[GUN_DUAL_SIMPLE], _("Dual simple gun")); - statusbar_add(statusbar, check[GUN_SCATTER], _("Scatter")); - statusbar_add(statusbar, check[GUN_TOMMY], _("Tommy")); - statusbar_add(statusbar, check[GUN_LASSER], _("Laser beam")); - statusbar_add(statusbar, check[GUN_MINE], _("Mine")); - statusbar_add(statusbar, check[GUN_BOMBBALL], _("Bomb ball")); - - statusbar_add(statusbar, check[BONUS_SPEED], _("Speed bonus")); - statusbar_add(statusbar, check[BONUS_SHOT], _("Infinite ammo")); - statusbar_add(statusbar, check[BONUS_TELEPORT], _("Unkillable bonus")); - statusbar_add(statusbar, check[BONUS_GHOST], _("No-walls bonus")); - statusbar_add(statusbar, check[BONUS_4X], _("1 shot to 4 directions bonus")); - statusbar_add(statusbar, check[BONUS_HIDDEN], _("Unvisibility bonus")); - - screen_register(screen_new("setting", screen_startSetting, setting_event, - setting_draw, stopScreenSetting)); - - initSettingFile(); - -#ifndef NO_SOUND - eventWidget(check_music); - eventWidget(check_sound); -#endif -} - -void public_server_get_setting_name_right(char *s) -{ - strcpy(s, text_field_get_text(textfield_name_player1)); -} - -void public_server_get_settingNameLeft(char *s) -{ - if (check_get_status(check_ai)) { - strcpy(s, NAME_AI); - } else { - strcpy(s, text_field_get_text(textfield_name_player2)); - } -} - -void public_server_get_settingCountRound(int *n) -{ - *n = atoi(text_field_get_text(textfield_count_cound)); -} - -bool_t setting_is_ai() -{ - return check_get_status(check_ai); -} - -bool_t setting_is_any_item() -{ - int i; - - for (i = GUN_DUAL_SIMPLE; i <= GUN_BOMBBALL; i++) { - if (check_get_status(check[i]) == TRUE) { - return TRUE; - } - } - - for (i = BONUS_SPEED; i < BONUS_HIDDEN; i++) { - if (check_get_status(check[i]) == TRUE) { - return TRUE; - } - } - - return FALSE; -} - -bool_t setting_is_item(int item) -{ - return check_get_status(check[item]); -} - -void setting_quit() -{ - int i; - - saveAndDestroyConfigFile(); - - wid_image_destroy(image_backgorund); - - label_destroy(label_weapons); - label_destroy(label_bonuses); - - label_destroy(label_count_round); - label_destroy(label_name_player1); - label_destroy(label_ai); - -#ifndef NO_SOUND - label_destroy(label_music); - label_destroy(label_sound); -#endif - label_destroy(label_name_player2); - text_field_destroy(textfield_name_player2); - - text_field_destroy(textfield_name_player1); - text_field_destroy(textfield_count_cound); - - wid_image_destroy(image_gun_dual_revolver); - wid_image_destroy(image_gun_scatter); - wid_image_destroy(image_gun_tommy); - wid_image_destroy(image_gun_lasser); - wid_image_destroy(image_gun_mine); - wid_image_destroy(image_gun_bombball); - - wid_image_destroy(image_bonus_speed); - wid_image_destroy(image_bonus_shot); - wid_image_destroy(image_bonus_teleport); - wid_image_destroy(image_bonus_ghost); - wid_image_destroy(image_bonus_4x); - wid_image_destroy(image_bonus_hidden); - -#ifndef NO_SOUND - check_destroy(check_music); - check_destroy(check_sound); -#endif - - check_destroy(check_ai); - - for (i = GUN_DUAL_SIMPLE; i <= GUN_BOMBBALL; i++) { - check_destroy(check[i]); - } - - for (i = BONUS_SPEED; i <= BONUS_HIDDEN; i++) { - check_destroy(check[i]); - } - - statusbar_destroy(statusbar); - - button_destroy(button_back); - button_destroy(button_keys); -} diff --git a/src/screen/setting.h b/src/screen/setting.h deleted file mode 100644 index c29e6a9..0000000 --- a/src/screen/setting.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef SCREEN_SETTING_H -#define SCREEN_SETTING_H - -#include "main.h" - -#define NAME_AI "TuxBot" -#define NAME_PLAYER_RIGHT "Name1" -#define NAME_PLAYER_LEFT "Name2" - -extern void setting_init(); -extern void setting_draw(); -extern void setting_event(); -extern void public_server_get_setting_name_right(char *s); -extern void public_server_get_settingNameLeft(char *s); -extern void public_server_get_settingCountRound(int *n); -extern bool_t setting_is_ai(); -extern bool_t setting_is_any_item(); -extern bool_t setting_is_item(int item); -extern void setting_quit(); - -#endif /* SCREEN_SETTING_H */ diff --git a/src/screen/settingKeys.c b/src/screen/settingKeys.c deleted file mode 100644 index da74b7b..0000000 --- a/src/screen/settingKeys.c +++ /dev/null @@ -1,392 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "textFile.h" -#include "director.h" -#include "homeDirector.h" - -#include "configFile.h" -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#include "sound.h" -#endif - -#include "mainMenu.h" -#include "setting.h" -#include "settingKeys.h" - -#include "widget.h" -#include "widget_button.h" -#include "widget_image.h" -#include "widget_catchkey.h" -#include "widget_label.h" - -static int keytable[KEY_LENGTH]; - -static textFile_t *keycontrolFile; -static widget_t *image_backgorund; -static widget_t *button_back; - -/* key names */ -static widget_t *tux_right; -static widget_t *tux_right_keyup; -static widget_t *tux_right_keydown; -static widget_t *tux_right_keyleft; -static widget_t *tux_right_keyright; -static widget_t *tux_right_keyswitch; -static widget_t *tux_right_keyfire; - -static widget_t *tux_left; -static widget_t *tux_left_keyup; -static widget_t *tux_left_keydown; - -static widget_t *tux_left_keyleft; -static widget_t *tux_left_keyright; -static widget_t *tux_left_keyswitch; -static widget_t *tux_left_keyfire; - -/* key values */ -static widget_t *tux_right_keyup_val; -static widget_t *tux_right_keydown_val; -static widget_t *tux_right_keyleft_val; -static widget_t *tux_right_keyright_val; -static widget_t *tux_right_keyswitch_val; -static widget_t *tux_right_keyfire_val; -static widget_t *tux_left_keyup_val; -static widget_t *tux_left_keydown_val; -static widget_t *tux_left_keyleft_val; -static widget_t *tux_left_keyright_val; -static widget_t *tux_left_keyswitch_val; -static widget_t *tux_left_keyfire_val; - - -/* TODO: add key images - * possible variant will be reusing widget_image for current widget_label so there won't - * be key names but only images - */ - -static void hotkey_escape() -{ - screen_set("setting"); -} - -void screen_startSettingKeys() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -static void setKeytableFromConfigFile(textFile_t *configFile) -{ - char val[STR_SIZE]; - - loadValueFromConfigFile(configFile, "TUX_RIGHT_MOVE_UP", val, STR_SIZE, "273"); - keytable[KEY_TUX_RIGHT_MOVE_UP] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_RIGHT_MOVE_RIGHT", val, STR_SIZE, "275"); - keytable[KEY_TUX_RIGHT_MOVE_RIGHT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_RIGHT_MOVE_LEFT", val, STR_SIZE, "276"); - keytable[KEY_TUX_RIGHT_MOVE_LEFT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_RIGHT_MOVE_DOWN", val, STR_SIZE, "274"); - keytable[KEY_TUX_RIGHT_MOVE_DOWN] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_RIGHT_SHOOT", val, STR_SIZE, "48"); - keytable[KEY_TUX_RIGHT_SHOOT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_RIGHT_SWITCH_WEAPON", val, STR_SIZE, "49"); - keytable[KEY_TUX_RIGHT_SWITCH_WEAPON] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_MOVE_UP", val, STR_SIZE, "119"); - keytable[KEY_TUX_LEFT_MOVE_UP] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_MOVE_RIGHT", val, STR_SIZE, "100"); - keytable[KEY_TUX_LEFT_MOVE_RIGHT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_MOVE_LEFT", val, STR_SIZE, "97"); - keytable[KEY_TUX_LEFT_MOVE_LEFT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_MOVE_DOWN", val, STR_SIZE, "115"); - keytable[KEY_TUX_LEFT_MOVE_DOWN] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_SHOOT", val, STR_SIZE, "113"); - keytable[KEY_TUX_LEFT_SHOOT] = atoi(val); - - loadValueFromConfigFile(configFile, "TUX_LEFT_SWITCH_WEAPON", val, STR_SIZE, "9"); - keytable[KEY_TUX_LEFT_SWITCH_WEAPON] = atoi(val); -} - -void key_table_init() -{ - char path[STR_PATH_SIZE]; - - sprintf(path, "%s%skeycontrol.conf", home_director_get(), PATH_SEPARATOR); - - keycontrolFile = text_file_load(path); - - if (keycontrolFile == NULL) { - error("Unable to load config file [%s]", path); - debug("Creating config file [%s]", path); - - keycontrolFile = text_file_new(path); - - if (keycontrolFile == NULL) { - error("Unable to create config file [%s]", path); - return; - } - } - - setKeytableFromConfigFile(keycontrolFile); -} - -int key_table_get_key(int n) -{ - /*printf("keytable[n] = %d\n", keytable[n]);*/ - return keytable[n]; -} - -void setting_key_draw() -{ - wid_image_draw(image_backgorund); - - button_draw(button_back); - - /* key names */ - label_draw(tux_right); - label_draw(tux_left); - label_draw(tux_right_keyup); - label_draw(tux_right_keydown); - label_draw(tux_right_keyleft); - label_draw(tux_right_keyright); - label_draw(tux_right_keyswitch); - label_draw(tux_right_keyfire); - label_draw(tux_left_keyup); - label_draw(tux_left_keydown); - label_draw(tux_left_keyleft); - label_draw(tux_left_keyright); - label_draw(tux_left_keyswitch); - label_draw(tux_left_keyfire); - - /* key values */ - catch_key_draw(tux_right_keyup_val); - catch_key_draw(tux_right_keydown_val); - catch_key_draw(tux_right_keyleft_val); - catch_key_draw(tux_right_keyright_val); - catch_key_draw(tux_right_keyswitch_val); - catch_key_draw(tux_right_keyfire_val); - catch_key_draw(tux_left_keyup_val); - catch_key_draw(tux_left_keydown_val); - catch_key_draw(tux_left_keyleft_val); - catch_key_draw(tux_left_keyright_val); - catch_key_draw(tux_left_keyswitch_val); - catch_key_draw(tux_left_keyfire_val); -} - -static void refreshKeytable() -{ - keytable[6] = catch_key_get(tux_left_keyup_val); - keytable[9] = catch_key_get(tux_left_keydown_val); - keytable[8] = catch_key_get(tux_left_keyleft_val); - keytable[7] = catch_key_get(tux_left_keyright_val); - keytable[10] = catch_key_get(tux_left_keyfire_val); - keytable[11] = catch_key_get(tux_left_keyswitch_val); - - keytable[0] = catch_key_get(tux_right_keyup_val); - keytable[3] = catch_key_get(tux_right_keydown_val); - keytable[2] = catch_key_get(tux_right_keyleft_val); - keytable[1] = catch_key_get(tux_right_keyright_val); - keytable[4] = catch_key_get(tux_right_keyfire_val); - keytable[5] = catch_key_get(tux_right_keyswitch_val); -} - -static void eventWidget(void *p) -{ - widget_t *button; - widget_t *catcher; - - button = (widget_t *) p; - catcher = (widget_t *) p; - - if (button == button_back) { - screen_set("setting"); - } - - /* events on value */ - if ((catcher == tux_left_keyup_val) || - (catcher == tux_left_keydown_val) || - (catcher == tux_left_keyleft_val) || - (catcher == tux_left_keyright_val) || - (catcher == tux_left_keyfire_val) || - (catcher == tux_left_keyswitch_val) || - (catcher == tux_right_keyup_val) || - (catcher == tux_right_keydown_val) || - (catcher == tux_right_keyleft_val) || - (catcher == tux_right_keyright_val) || - (catcher == tux_right_keyswitch_val) || - (catcher == tux_right_keyfire_val)) { - /* draw press any key picture - this will be done by widget catchkey - check if there is no other key with new value (what to revert if yes?) - */ - - refreshKeytable(); - } - -} - -void setting_key_event() -{ - button_event(button_back); - - catch_key_event(tux_right_keyup_val); - catch_key_event(tux_right_keydown_val); - catch_key_event(tux_right_keyleft_val); - catch_key_event(tux_right_keyright_val); - catch_key_event(tux_right_keyswitch_val); - catch_key_event(tux_right_keyfire_val); - - catch_key_event(tux_left_keyup_val); - catch_key_event(tux_left_keydown_val); - catch_key_event(tux_left_keyleft_val); - catch_key_event(tux_left_keyright_val); - catch_key_event(tux_left_keyswitch_val); - catch_key_event(tux_left_keyfire_val); -} - -void stopScreenSettingKeys() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -void setting_key_int() -{ - key_table_init(); - - image_t *image = image_get(IMAGE_GROUP_BASE, "screen_main"); - - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, eventWidget); - - tux_left = label_new(_("Player 2"), WINDOW_SIZE_X / 4, 150, WIDGET_LABEL_CENTER); - tux_left_keyup = label_new(_("Up:"), 50, tux_left->y + 50, WIDGET_LABEL_LEFT); - tux_left_keydown = label_new(_("Down:"), tux_left_keyup->x, tux_left_keyup->y + 20, WIDGET_LABEL_LEFT); - tux_left_keyleft = label_new(_("Left:"), tux_left_keyup->x, tux_left_keydown->y + 20, WIDGET_LABEL_LEFT); - tux_left_keyright = label_new(_("Right:"), tux_left_keyup->x, tux_left_keyleft->y + 20, WIDGET_LABEL_LEFT); - tux_left_keyfire = label_new(_("Fire:"), tux_left_keyup->x, tux_left_keyright->y + 20, WIDGET_LABEL_LEFT); - tux_left_keyswitch = label_new(_("Switch gun:"), tux_left_keyup->x, tux_left_keyfire->y + 20, WIDGET_LABEL_LEFT); - tux_right = label_new(_("Player 1"), WINDOW_SIZE_X / 2 + WINDOW_SIZE_X / 4, tux_left->y, WIDGET_LABEL_CENTER); - tux_right_keyup = label_new(_("Up:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_left->y + 50, WIDGET_LABEL_LEFT); - tux_right_keydown = label_new(_("Down:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_left_keyup->y + 20, WIDGET_LABEL_LEFT); - tux_right_keyleft = label_new(_("Left:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_right_keydown->y + 20, WIDGET_LABEL_LEFT); - tux_right_keyright = label_new(_("Right:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_right_keyleft->y + 20, WIDGET_LABEL_LEFT); - tux_right_keyfire = label_new(_("Fire:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_left_keyright->y + 20, WIDGET_LABEL_LEFT); - tux_right_keyswitch = label_new(_("Switch gun:"), WINDOW_SIZE_X / 2 + tux_left_keyup->x, tux_left_keyfire->y + 20, WIDGET_LABEL_LEFT); - - tux_left_keyup_val = catch_key_new(keytable[6], tux_left_keyup->x + 200, tux_left->y + 55, eventWidget); - tux_left_keydown_val = catch_key_new(keytable[9], tux_left_keyup_val->x, tux_left_keyup->y + 25, eventWidget); - tux_left_keyleft_val = catch_key_new(keytable[8], tux_left_keyup_val->x, tux_left_keydown->y + 25, eventWidget); - tux_left_keyright_val = catch_key_new(keytable[7], tux_left_keyup_val->x, tux_left_keyleft->y + 25, eventWidget); - tux_left_keyfire_val = catch_key_new(keytable[10], tux_left_keyup_val->x, tux_left_keyright->y + 25, eventWidget); - tux_left_keyswitch_val = catch_key_new(keytable[11], tux_left_keyup_val->x, tux_left_keyfire->y + 25, eventWidget); - tux_right_keyup_val = catch_key_new(keytable[0], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_left->y + 55, eventWidget); - tux_right_keydown_val = catch_key_new(keytable[3], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_left_keyup->y + 25, eventWidget); - tux_right_keyleft_val = catch_key_new(keytable[2], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_right_keydown->y + 25, eventWidget); - tux_right_keyright_val = catch_key_new(keytable[1], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_right_keyleft->y + 25, eventWidget); - tux_right_keyfire_val = catch_key_new(keytable[4], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_left_keyright->y + 25, eventWidget); - tux_right_keyswitch_val = catch_key_new(keytable[5], WINDOW_SIZE_X / 2 + tux_left_keyup_val->x, tux_left_keyfire->y + 25, eventWidget); - - screen_register(screen_new("settingKeys", screen_startSettingKeys, setting_key_event, setting_key_draw, stopScreenSettingKeys)); -} - -void key_table_quit() -{ - text_file_destroy(keycontrolFile); -} - -void saveKeyTable(textFile_t *configFile) -{ - char str[STR_SIZE]; - - sprintf(str, "%d", catch_key_get(tux_left_keyup_val)); - setValueInConfigFile(configFile, "TUX_LEFT_MOVE_UP", str); - sprintf(str, "%d", catch_key_get(tux_left_keydown_val)); - setValueInConfigFile(configFile, "TUX_LEFT_MOVE_DOWN", str); - sprintf(str, "%d", catch_key_get(tux_left_keyleft_val)); - setValueInConfigFile(configFile, "TUX_LEFT_MOVE_LEFT", str); - sprintf(str, "%d", catch_key_get(tux_left_keyright_val)); - setValueInConfigFile(configFile, "TUX_LEFT_MOVE_RIGHT", str); - sprintf(str, "%d", catch_key_get(tux_left_keyfire_val)); - setValueInConfigFile(configFile, "TUX_LEFT_SHOOT", str); - sprintf(str, "%d", catch_key_get(tux_left_keyswitch_val)); - setValueInConfigFile(configFile, "TUX_LEFT_SWITCH_WEAPON", str); - - sprintf(str, "%d", catch_key_get(tux_right_keyup_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_MOVE_UP", str); - sprintf(str, "%d", catch_key_get(tux_right_keydown_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_MOVE_DOWN", str); - sprintf(str, "%d", catch_key_get(tux_right_keyleft_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_MOVE_LEFT", str); - sprintf(str, "%d", catch_key_get(tux_right_keyright_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_MOVE_RIGHT", str); - sprintf(str, "%d", catch_key_get(tux_right_keyfire_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_SHOOT", str); - sprintf(str, "%d", catch_key_get(tux_right_keyswitch_val)); - setValueInConfigFile(configFile, "TUX_RIGHT_SWITCH_WEAPON", str); - - text_file_save(configFile); -} - -void setting_key_quit() -{ - saveKeyTable(keycontrolFile); - - key_table_quit(); - - wid_image_destroy(image_backgorund); - - /* key names */ - label_destroy(tux_right); - label_destroy(tux_left); - label_destroy(tux_right_keyup); - label_destroy(tux_right_keydown); - label_destroy(tux_right_keyleft); - label_destroy(tux_right_keyright); - label_destroy(tux_right_keyswitch); - label_destroy(tux_right_keyfire); - label_destroy(tux_left_keyup); - label_destroy(tux_left_keydown); - label_destroy(tux_left_keyright); - label_destroy(tux_left_keyleft); - label_destroy(tux_left_keyswitch); - label_destroy(tux_left_keyfire); - - /* key values */ - catch_key_destroy(tux_right_keyup_val); - catch_key_destroy(tux_right_keydown_val); - catch_key_destroy(tux_right_keyleft_val); - catch_key_destroy(tux_right_keyright_val); - catch_key_destroy(tux_right_keyswitch_val); - catch_key_destroy(tux_right_keyfire_val); - catch_key_destroy(tux_left_keyup_val); - catch_key_destroy(tux_left_keydown_val); - catch_key_destroy(tux_left_keyleft_val); - catch_key_destroy(tux_left_keyright_val); - catch_key_destroy(tux_left_keyswitch_val); - catch_key_destroy(tux_left_keyfire_val); - - button_destroy(button_back); -} diff --git a/src/screen/settingKeys.h b/src/screen/settingKeys.h deleted file mode 100644 index c7d6c66..0000000 --- a/src/screen/settingKeys.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef SCREEN_SETTING_KEYS_H -#define SCREEN_SETTING_KEYS_H - -#define KEY_TUX_RIGHT_MOVE_UP 0 -#define KEY_TUX_RIGHT_MOVE_RIGHT 1 -#define KEY_TUX_RIGHT_MOVE_LEFT 2 -#define KEY_TUX_RIGHT_MOVE_DOWN 3 -#define KEY_TUX_RIGHT_SHOOT 4 -#define KEY_TUX_RIGHT_SWITCH_WEAPON 5 -#define KEY_TUX_LEFT_MOVE_UP 6 -#define KEY_TUX_LEFT_MOVE_RIGHT 7 -#define KEY_TUX_LEFT_MOVE_LEFT 8 -#define KEY_TUX_LEFT_MOVE_DOWN 9 -#define KEY_TUX_LEFT_SHOOT 10 -#define KEY_TUX_LEFT_SWITCH_WEAPON 11 -#define KEY_LENGTH 12 - -#include "main.h" - -extern void setting_key_int(); -extern void setting_key_draw(); -extern void setting_key_event(); -extern void setting_key_quit(); -extern void key_table_init(); -extern int key_table_get_key(int n); -extern void key_table_quit(); - -#endif /* SCREEN_SETTING_KEYS_H */ diff --git a/src/screen/table.c b/src/screen/table.c deleted file mode 100644 index 34b4680..0000000 --- a/src/screen/table.c +++ /dev/null @@ -1,228 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "homeDirector.h" - -#include "interface.h" -#include "screen.h" -#include "image.h" -#include "hotKey.h" - -#ifndef NO_SOUND -#include "music.h" -#endif - -#include "table.h" - -#include "widget_image.h" -#include "widget_label.h" -#include "widget_button.h" - -static widget_t *image_backgorund; -static widget_t *button_back; - -static list_t *listWidgetLabelNumer; -static list_t *listWidgetLabelName; -static list_t *listWidgetLabelScore; - -static textFile_t *textFile; - -static void hotkey_escape() -{ - screen_set("mainMenu"); -} - -void table_start() -{ -#ifndef NO_SOUND - music_play("menu", MUSIC_GROUP_BASE); -#endif - - hot_key_register(SDLK_ESCAPE, hotkey_escape); -} - -void table_draw() -{ - int i; - - wid_image_draw(image_backgorund); - - button_draw(button_back); - - for (i = 0; i < listWidgetLabelNumer->count; i++) { - widget_t *this; - this = (widget_t *) listWidgetLabelNumer->list[i]; - label_draw(this); - } - - for (i = 0; i < listWidgetLabelName->count; i++) { - widget_t *this; - this = (widget_t *) listWidgetLabelName->list[i]; - label_draw(this); - } - - for (i = 0; i < listWidgetLabelScore->count; i++) { - widget_t *this; - this = (widget_t *) listWidgetLabelScore->list[i]; - label_draw(this); - } -} - -void table_event() -{ - button_event(button_back); -} - -void table_stop() -{ - hot_key_unregister(SDLK_ESCAPE); -} - -static void eventWidget(void *p) -{ - widget_t *button; - - button = (widget_t *) p; - - if (button == button_back) { - screen_set("mainMenu"); - } -} - -static void setWidgetLabel() -{ - int i; - - if (textFile == NULL) { - error("Didn't load the high score file [%s]", SCREEN_TABLE_FILE_HIGHSCORE_NAME); - - return; - } - - if (listWidgetLabelNumer != NULL || - listWidgetLabelName != NULL || - listWidgetLabelScore != NULL) { - list_destroy_item(listWidgetLabelNumer, label_destroy); - list_destroy_item(listWidgetLabelName, label_destroy); - list_destroy_item(listWidgetLabelScore, label_destroy); - } - - listWidgetLabelNumer = list_new(); - listWidgetLabelName = list_new(); - listWidgetLabelScore = list_new(); - - for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) { - widget_t *label; - char name[STR_NAME_SIZE]; - char score[STR_NUM_SIZE]; - char num[STR_NUM_SIZE]; - char *line; - - line = (char *) textFile->text->list[i]; - - sscanf(line, "%s %s", name, score); - sprintf(num, "%2d)", i + 1); - - label = label_new(num, WINDOW_SIZE_X / 2 - 100, 200 + i * 20, WIDGET_LABEL_LEFT); - list_add(listWidgetLabelNumer, label); - - label = label_new(name, WINDOW_SIZE_X / 2, 200 + i * 20, WIDGET_LABEL_CENTER); - list_add(listWidgetLabelName, label); - - label = label_new(score, WINDOW_SIZE_X / 2 + 80, 200 + i * 20, WIDGET_LABEL_LEFT); - list_add(listWidgetLabelScore, label); - } -} - -static void loadHighscoreFile() -{ - char path[STR_PATH_SIZE]; - int i; - - sprintf(path, "%s%s%s", home_director_get(), PATH_SEPARATOR, SCREEN_TABLE_FILE_HIGHSCORE_NAME); - textFile = text_file_load(path); - - if (textFile == NULL) { - error("Unable to load the high score file [%s]", path); - debug("Creating the high score file [%s]", path); - textFile = text_file_new(path); - } else { - return; - } - - if (textFile == NULL) { - error("Unable to create the high score file [%s]", path); - return; - } - - for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) { - list_add(textFile->text, strdup("--- 0")); - } - - text_file_save(textFile); -} - -int table_add(char *name, int score) -{ - int i; - - for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) { - char *line; - char thisName[STR_NAME_SIZE]; - int thisCore; - - line = (char *) textFile->text->list[i]; - sscanf(line, "%s %d", thisName, &thisCore); - - if (score >= thisCore) { - char new[STR_SIZE]; - - sprintf(new, "%s %d", name, score); - list_ins(textFile->text, i, strdup(new)); - list_del_item(textFile->text, SCREEN_TABLE_MAX_PLAYERS, free); - setWidgetLabel(); - - return 0; - } - } - - return -1; -} - -void table_init() -{ - image_t *image; - - image = image_add("screen_table.png", IMAGE_NO_ALPHA, "screen_table", IMAGE_GROUP_BASE); - image_backgorund = wid_image_new(0, 0, image); - - button_back = button_new(_("Back"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2, - WINDOW_SIZE_Y - 100, eventWidget); - - loadHighscoreFile(); - listWidgetLabelNumer = NULL; - listWidgetLabelName = NULL; - listWidgetLabelScore = NULL; - setWidgetLabel(); - - screen_register(screen_new("table", table_start, table_event, table_draw, table_stop)); -} - -void table_quit() -{ - wid_image_destroy(image_backgorund); - - button_destroy(button_back); - list_destroy_item(listWidgetLabelNumer, label_destroy); - list_destroy_item(listWidgetLabelName, label_destroy); - list_destroy_item(listWidgetLabelScore, label_destroy); - - if (textFile != NULL) { - text_file_save(textFile); - text_file_destroy(textFile); - } -} diff --git a/src/screen/table.h b/src/screen/table.h deleted file mode 100644 index 7940ee9..0000000 --- a/src/screen/table.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SCREEN_TABLE_H -#define SCREEN_TABLE_H - -#define SCREEN_TABLE_MAX_PLAYERS 10 -#define SCREEN_TABLE_FILE_HIGHSCORE_NAME "highscore" - -extern void table_start(); -extern void table_draw(); -extern void table_event(); -extern void table_stop(); -extern int table_add(char *name, int score); -extern void table_init(); -extern void table_quit(); - -#endif /* SCREEN_TABLE_H */ diff --git a/src/screen/world.c b/src/screen/world.c deleted file mode 100644 index 90d10a1..0000000 --- a/src/screen/world.c +++ /dev/null @@ -1,536 +0,0 @@ -#include <stdio.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "item.h" -#include "shot.h" -#include "gun.h" -#include "myTimer.h" -#include "net_multiplayer.h" -#include "arena.h" -#include "arenaFile.h" -#include "proto.h" -#include "modules.h" -#include "idManager.h" -#include "serverSendMsg.h" - -#include "interface.h" -#include "layer.h" -#include "image.h" -#include "font.h" - -#include "hotKey.h" -#include "panel.h" -#include "radar.h" -#include "chat.h" -#include "pauza.h" -#include "term.h" -#include "saveDialog.h" -#include "yes_no_dialog.h" -#include "saveLoad.h" - -#ifndef NO_SOUND -#include "music.h" -#include "sound.h" -#endif /* NO_SOUND */ - -#include "screen.h" -#include "world.h" -#include "analyze.h" -#include "setting.h" -#include "gameType.h" -#include "settingKeys.h" -#include "choiceArena.h" -#include "table.h" -#include "control.h" - -static arena_t *arena; -static bool_t isScreenWorldInit = FALSE; -static bool_t isEndWorld; - -static tux_t *tuxWithControlRightKeyboard; -static tux_t *tuxWithControlLeftKeyboard; - -static control_t *control_tux_right; -static control_t *control_tux_left; - -bool_t world_is_inicialized() -{ - return isScreenWorldInit; -} - -void setGameType() -{ - int ret = 0; - - ret = net_multiplayer_init(public_server_get_settingGameType(), public_server_get_settingIP(), - public_server_get_settingPort(), public_server_get_settingProto()); - - if (ret != 0) { - error("Unable to initialize network"); - world_do_end(); - } -} - -void world_set_arena(arenaFile_t *arenaFile) -{ - arena = arena_file_get_arena(arenaFile); - -#ifndef NO_SOUND - music_play(arena->music, MUSIC_GROUP_USER); -#endif /* NO_SOUND */ -} - -void world_do_end() -{ - isEndWorld = TRUE; -} - -static void timer_endArena() -{ - if (net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) { - world_do_end(); - return; - } -} - -void world_inc_round() -{ - if (arena->max_countRound == WORLD_COUNT_ROUND_UNLIMITED || - arena->countRound >= arena->max_countRound) { - return; - } - - arena->countRound++; - /*printf("count %d/%d\n", count, max_count);*/ - - if (arena->countRound >= arena->max_countRound) { - /*printf("count %d ending\n", count);*/ -#ifndef NO_SOUND - sound_play("end", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - timer_add_task(arena_get_current()->listTimer, TIMER_ONE, timer_endArena, NULL, TIMER_END_ARENA); - } -} - -bool_t world_is_match_end() -{ - if (arena->max_countRound == WORLD_COUNT_ROUND_UNLIMITED) { - return FALSE; - } - - return (arena->countRound >= arena->max_countRound); -} - -void prepareArena() -{ - tux_t *tux; - char name[STR_NAME_SIZE]; - - /*printf("public_server_get_settingAI = %s\n", public_server_get_settingAI());*/ - arena_set_current(NULL); - tuxWithControlRightKeyboard = NULL; - tuxWithControlLeftKeyboard = NULL; - - control_tux_right = NULL; - control_tux_left = NULL; - - if (game_type_load_session() != NULL) { - load_arena(game_type_load_session()); - return; - } - - switch (net_multiplayer_get_game_type()) { - case NET_GAME_TYPE_NONE: - world_set_arena(choice_arena_get()); - item_add_new_item(arena->spaceItem, ID_UNKNOWN); - public_server_get_settingCountRound(&arena->max_countRound); - - tux = tux_new(); - - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_RIGHT); - public_server_get_setting_name_right(name); - tux_set_name(tux, name); - space_add(arena->spaceTux, tux); - - tux = tux_new(); - - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_LEFT); - public_server_get_settingNameLeft(name); - tux_set_name(tux, name); - space_add(arena->spaceTux, tux); - - if (setting_is_ai()) { - tux->control = TUX_CONTROL_AI; - - if (module_load("libmodAI") != 0) { - tux->control = TUX_CONTROL_KEYBOARD_LEFT; - } - } - /*printf("game_type_load_session = %s\n", game_type_load_session());*/ - - break; - - case NET_GAME_TYPE_SERVER: - world_set_arena(choice_arena_get()); - item_add_new_item(arena->spaceItem, ID_UNKNOWN); - public_server_get_settingCountRound(&arena->max_countRound); - - tux = tux_new(); - - world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_RIGHT); - public_server_get_setting_name_right(name); - tux_set_name(tux, name); - space_add(arena->spaceTux, tux); - break; - - case NET_GAME_TYPE_CLIENT: - break; - } -} - -void world_draw() -{ - if (arena != NULL) { - arena_draw(arena); - panel_draw(tuxWithControlRightKeyboard, tuxWithControlLeftKeyboard); - - if (arena->w > WINDOW_SIZE_X || arena->h > WINDOW_SIZE_Y) { - radar_draw(arena); - } - } - - chat_draw(); - pauza_draw(); - term_draw(); - save_dialog_draw(); - yes_no_dialog_draw(); -} - -static void netAction(tux_t *tux, int action) -{ - if (public_server_get_settingGameType() == NET_GAME_TYPE_SERVER) { - proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, NULL, tux, action); - } - - if (public_server_get_settingGameType() == NET_GAME_TYPE_CLIENT) { - proto_send_event_client(action); - } -} - -static void control_keyboard(tux_t *tux, control_t *control) -{ - switch (control_get_key_route(control)) { - case CONTROL_UP: - netAction(tux, TUX_UP); - tux_action(tux, TUX_UP); - break; - case CONTROL_RIGHT: - netAction(tux, TUX_RIGHT); - tux_action(tux, TUX_RIGHT); - break; - case CONTROL_LEFT: - netAction(tux, TUX_LEFT); - tux_action(tux, TUX_LEFT); - break; - case CONTROL_DOWN: - netAction(tux, TUX_DOWN); - tux_action(tux, TUX_DOWN); - break; - } - - switch (control_get_key_action(control)) { - case CONTROL_SHOT: - if (tux->isCanShot == TRUE) { - netAction(tux, TUX_SHOT); - tux_action(tux, TUX_SHOT); - } - break; - case CONTROL_SWITCH: - if (tux->isCanSwitchGun == TRUE) { - netAction(tux, TUX_SWITCH_GUN); - tux_action(tux, TUX_SWITCH_GUN); - } - break; - } -} - -static void eventEnd() -{ - if (isEndWorld == TRUE) { - screen_set("analyze"); - return; - } -} - -tux_t *world_get_control_tux(int control_type) -{ - switch (control_type) { - case TUX_CONTROL_KEYBOARD_RIGHT: - return tuxWithControlRightKeyboard; - break; - case TUX_CONTROL_KEYBOARD_LEFT: - return tuxWithControlLeftKeyboard; - break; - } - - return NULL; -} - -void world_set_control_tux(tux_t *tux, int control_type) -{ - switch (control_type) { - case TUX_CONTROL_KEYBOARD_RIGHT: - tuxWithControlRightKeyboard = tux; - tux->control = control_type; - - control_tux_right = control_new( - key_table_get_key(KEY_TUX_RIGHT_MOVE_UP), - key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT), - key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT), - key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN), - key_table_get_key(KEY_TUX_RIGHT_SHOOT), - key_table_get_key(KEY_TUX_RIGHT_SWITCH_WEAPON) - ); - - break; - case TUX_CONTROL_KEYBOARD_LEFT: - tuxWithControlLeftKeyboard = tux; - tux->control = control_type; - - control_tux_left = control_new( - key_table_get_key(KEY_TUX_LEFT_MOVE_UP), - key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT), - key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT), - key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN), - key_table_get_key(KEY_TUX_LEFT_SHOOT), - key_table_get_key(KEY_TUX_LEFT_SWITCH_WEAPON) - ); - - break; - } -} - -void world_tux_control(tux_t *p) -{ - assert(p != NULL); - - if (p->status != TUX_STATUS_ALIVE) { - return; - } - - switch (p->control) { - case TUX_CONTROL_NONE: - fatal("Controls not defined"); - break; - - case TUX_CONTROL_KEYBOARD_RIGHT: - if (chat_is_active() == FALSE) { - control_keyboard(tuxWithControlRightKeyboard, control_tux_right); - } - break; - - case TUX_CONTROL_KEYBOARD_LEFT: - if (chat_is_active() == FALSE) { - control_keyboard(tuxWithControlLeftKeyboard, control_tux_left); - } - break; - } -} - -void world_event() -{ - tux_t *thisTux; - - if (arena == NULL) { - net_multiplayer_event(); - eventEnd(); - return; - } - - net_multiplayer_event(); - - if (pauza_is_active() == FALSE && - save_dialog_is_active() == FALSE && - yes_no_dialog_is_active() == FALSE) { - arena_event(arena); - /*module_event();*/ - } - - thisTux = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - - radar_add(thisTux->id, thisTux->x, thisTux->y, RADAR_TYPE_YOU); - - thisTux = world_get_control_tux(TUX_CONTROL_KEYBOARD_LEFT); - - if (thisTux != NULL) { - radar_add(thisTux->id, thisTux->x, thisTux->y, RADAR_TYPE_TUX); - } - - eventEnd(); - chat_event(); - pauza_event(); - term_event(); - save_dialog_event(); - yes_no_dialog_event(); -} - -void dialog_yes(void *p) -{ - world_do_end(); -} - -void dialog_no(void *p) -{ -} - -static void hotkey_escape() -{ - yes_no_dialog_set(_("Do you really want to quit the game?"), dialog_yes, dialog_no, NULL); - yes_no_dialog_set_active(TRUE); - /*world_do_end();*/ -} - -void startWorld() -{ - arena = NULL; - isEndWorld = FALSE; - - hot_key_register(SDLK_ESCAPE, hotkey_escape); - arena_init(); - id_init_list(); - radar_init(); - pauza_init(); - term_init(); - - setGameType(); - - yes_no_dialog_init(); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_NONE) { - save_dialog_init(); - } - - module_init(); - chat_init(); - prepareArena(); -} - -static void action_analyze(space_t *space, tux_t *tux, void *p) -{ - analyze_add(tux->name, tux->score); -} - -static void setAnalyze() -{ - analyze_restart(); - - space_action(arena->spaceTux, action_analyze, NULL); - - analyze_end(); -} - -static void setTable() -{ - tux_t *tuxRight; - tux_t *tuxLeft; - - if (public_server_get_settingGameType() != NET_GAME_TYPE_NONE) { - return; - } - - tuxRight = world_get_control_tux(TUX_CONTROL_KEYBOARD_RIGHT); - tuxLeft = world_get_control_tux(TUX_CONTROL_KEYBOARD_LEFT); - - if (tuxRight->score > tuxLeft->score) { - table_add(tuxRight->name, tuxRight->score); - } - - if (tuxLeft->score > tuxRight->score) { - table_add(tuxLeft->name, tuxLeft->score); - } -} - -void stoptWorld() -{ - if (arena != NULL) { - setTable(); - setAnalyze(); - } - - hot_key_unregister(SDLK_ESCAPE); - arena_quit(); - - radar_quit(); - pauza_quit(); - term_quit(); - - yes_no_dialog_quit(); - - if (net_multiplayer_get_game_type() == NET_GAME_TYPE_NONE) { - save_dialog_quit(); - } - - if (control_tux_right != NULL) { - control_destroy(control_tux_right); - } - - if (control_tux_left != NULL) { - control_destroy(control_tux_left); - } - -#ifndef NO_SOUND - music_stop(); -#endif /* NO_SOUND */ - image_del_all_image_in_group(IMAGE_GROUP_USER); - -#ifndef NO_SOUND - music_del_all_in_group(MUSIC_GROUP_USER); -#endif /* NO_SOUND */ - module_quit(); - chat_quit(); - - net_multiplayer_quit(); - - if (arena != NULL) { - arena_destroy(arena); - } - - id_quit_list(); -} - -void world_init() -{ - assert(image_is_inicialized() == TRUE); - assert(tux_is_inicialized() == TRUE); - assert(shot_is_inicialized() == TRUE); - assert(panel_is_inicialized() == TRUE); - assert(screen_is_inicialized() == TRUE); - - screen_register(screen_new("world", startWorld, world_event, world_draw, stoptWorld)); - -#ifndef NO_SOUND - sound_add("dead.ogg", "dead", SOUND_GROUP_BASE); - sound_add("explozion.ogg", "explozion", SOUND_GROUP_BASE); - sound_add("gun_lasser.ogg", "gun_lasser", SOUND_GROUP_BASE); - sound_add("gun_revolver.ogg", "gun_revolver", SOUND_GROUP_BASE); - sound_add("gun_scatter.ogg", "gun_scatter", SOUND_GROUP_BASE); - sound_add("gun_tommy.ogg", "gun_tommy", SOUND_GROUP_BASE); - sound_add("put_mine.ogg", "put_mine", SOUND_GROUP_BASE); - sound_add("teleport.ogg", "teleport", SOUND_GROUP_BASE); - sound_add("item_bonus.ogg", "item_bonus", SOUND_GROUP_BASE); - sound_add("item_gun.ogg", "item_gun", SOUND_GROUP_BASE); - sound_add("switch_gun.ogg", "switch_gun", SOUND_GROUP_BASE); - sound_add("end.ogg", "end", SOUND_GROUP_BASE); -#endif /* NO_SOUND */ - - isScreenWorldInit = TRUE; -} - -void world_quit() -{ - debug("Shutting down the world screen"); - isScreenWorldInit = FALSE; -} diff --git a/src/screen/world.h b/src/screen/world.h deleted file mode 100644 index f64ac93..0000000 --- a/src/screen/world.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SCREEN_WORLD_H -#define SCREEN_WORLD_H - -#include "main.h" -#include "myTimer.h" -#include "tux.h" -#include "arenaFile.h" -#include "arena.h" - -#define TIMER_END_ARENA 5000 -#define WORLD_COUNT_ROUND_UNLIMITED -1 -#define LAG_SERVER_UNKNOWN -1 - -extern bool_t world_is_inicialized(); -extern void world_init(); -extern void world_set_arena(arenaFile_t *arenaFile); -extern void world_do_end(); -extern void world_inc_round(); -extern bool_t world_is_match_end(); -extern tux_t *world_get_control_tux(int control_type); -extern void world_set_control_tux(tux_t *tux, int control_type); -extern void world_tux_control(tux_t *p); -extern void world_draw(); -extern void world_event(); -extern void world_start(); -extern void world_stop(); -extern void world_quit(); - -#endif /* SCREEN_WORLD_H */ diff --git a/src/screens/main_menu.c b/src/screens/main_menu.c new file mode 100644 index 0000000..3fe8a0c --- /dev/null +++ b/src/screens/main_menu.c @@ -0,0 +1,33 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "text.h" +#include "scale.h" +#include "teapot.h" + +void ta_screens_main_menu_update(void) { +} + +void ta_screens_main_menu_upload(SDL_GPUCommandBuffer *command_buffer) { + ta_teapot_upload(command_buffer); +} + +void ta_screens_main_menu_render(SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass) { + ta_teapot_render(command_buffer, render_pass); +}
\ No newline at end of file diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/server/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/server/highScore.c b/src/server/highScore.c deleted file mode 100644 index 75323fa..0000000 --- a/src/server/highScore.c +++ /dev/null @@ -1,87 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "highScore.h" - -static textFile_t *textFile; - -void high_score_init(char *file) -{ - int i; - - textFile = text_file_load(file); - - if (textFile == NULL) { - error("Unable to load high score [%s]", file); - debug("Creating high score file [%s]", file); - textFile = text_file_new(file); - } else { - debug("Loading high score file [%s]", file); - return; - } - - if (textFile == NULL) { - error("Unable to create high score file [%s]", file); - return; - } - - for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) { - list_add(textFile->text, strdup("--- 0")); - } - - text_file_save(textFile); -} - -int table_add(char *name, int score) -{ - int i; - - if (score <= 0) { - return -1; /* ha ha ha */ - } - - for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) { - char *line; - char thisName[STR_NAME_SIZE]; - int thisCore; - - line = (char *) textFile->text->list[i]; - sscanf(line, "%s %d", thisName, &thisCore); - - if (score >= thisCore) { - char new[STR_SIZE]; - - sprintf(new, "%s %d", name, score); - list_ins(textFile->text, i, strdup(new)); - list_del_item(textFile->text, HIGHSCORE_MAX_PLAYERS, free); - /*text_file_print(textFile);*/ - text_file_save(textFile); - - return 0; - } - } - - return -1; -} - -char *high_score_get_table(int index) -{ - if (textFile != NULL && index >= 0 && index < textFile->text->count) { - return (char *) textFile->text->list[index]; - } - - return NULL; -} - -void high_score_quit() -{ - if (textFile != NULL) { - text_file_save(textFile); - text_file_destroy(textFile); - } -} diff --git a/src/server/highScore.h b/src/server/highScore.h deleted file mode 100644 index 604afb7..0000000 --- a/src/server/highScore.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef HIGH_SCORE_H -#define HIGH_SCORE_H - -#define HIGHSCORE_MAX_PLAYERS 100 - -extern void high_score_init(char *file); -extern int table_add(char *name, int score); -extern char *high_score_get_table(int index); -extern void high_score_quit(); - -#endif /* HIGH_SCORE_H */ diff --git a/src/server/log.c b/src/server/log.c deleted file mode 100644 index 00cde64..0000000 --- a/src/server/log.c +++ /dev/null @@ -1,79 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <time.h> -#include <assert.h> - -#include "log.h" -#include "main.h" - -static FILE *logFile; - -int log_init(char *name) -{ - logFile = fopen(name, "a"); - - if (logFile == NULL) { - error("Unable to open log file [%s]", name); - return -1; - } - - debug("Using log file [%s]", name); - - log_add(LOG_INF, "Logging started"); - - return 0; -} - -void log_add(int type, char *msg) -{ - char str[STR_LOG_SIZE]; - struct tm *tm_struct; - time_t currentTime; - char *str_type; - - if (logFile == NULL) { - return; - } - - currentTime = time(NULL); - tm_struct = localtime(¤tTime); - - switch (type) { - case LOG_INF: - str_type = "INFO"; - break; - case LOG_DBG: - str_type = "DEBUG"; - break; - case LOG_WRN: - str_type = "WARN"; - break; - case LOG_ERR: - str_type = "ERROR"; - break; - default: - fatal("Unknown type of the logging string"); - break; - } - - sprintf(str, "[%02d-%02d-%02d %02d:%02d:%02d] [%s] %s\n", - 1900 + tm_struct->tm_year, tm_struct->tm_mon + 1, tm_struct->tm_mday, - tm_struct->tm_hour, tm_struct->tm_min, tm_struct->tm_sec, - str_type, msg); - - fprintf(logFile, "%s", str); - - fflush(logFile); -} - -void log_quit() -{ - if (logFile == NULL) { - return; - } - - log_add(LOG_INF, "Logging finished"); - fclose(logFile); -} diff --git a/src/server/log.h b/src/server/log.h deleted file mode 100644 index b256e08..0000000 --- a/src/server/log.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef LOG_H -#define LOG_H - -#define LOG_INF 1 -#define LOG_DBG 2 -#define LOG_WRN 3 -#define LOG_ERR 4 - -extern int log_init(char *name); -extern void log_add(int type, char *msg); -extern void log_quit(); - -#endif /* LOG_H */ diff --git a/src/server/publicServer.c b/src/server/publicServer.c deleted file mode 100644 index ba97f7d..0000000 --- a/src/server/publicServer.c +++ /dev/null @@ -1,450 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <signal.h> -#include <time.h> - -#ifndef __WIN32__ -#include <sys/socket.h> -#include <sys/select.h> -#include <arpa/inet.h> -#else /* __WIN32__ */ -#include <windows.h> -#include <wininet.h> -#endif /* __WIN32__ */ - -#include <unistd.h> -#include <fcntl.h> -#include <errno.h> - -#include <sys/types.h> -#include <sys/stat.h> - -#include "main.h" -#include "list.h" -#include "tux.h" -#include "idManager.h" -#include "myTimer.h" -#include "arena.h" -#include "item.h" -#include "shot.h" -#include "arenaFile.h" -#include "proto.h" -#include "modules.h" -#include "net_multiplayer.h" - -#include "publicServer.h" -#include "serverConfigFile.h" -#include "highScore.h" -#include "log.h" - -#include "dns.h" - -static arena_t *arena; -static bool_t isSignalEnd; -static arenaFile_t *choice_arenaFile; - -extern int errno; - -#define __PACKED__ __attribute__ ((__packed__)) - -void world_inc_round() -{ -} - -char *public_server_get_setting(char *env, char *param, char *default_val) -{ - return getParamElse(param, server_configFile_get_value(env, default_val)); -} - -/** - * Send the process to the background - */ -static void daemonize() -{ -#ifndef __WIN32__ - pid_t pid, sid; - - FILE *pid_file; - - /* are we already a daemon? */ - if (getppid() == 1) { - return; - } - - /* fork off the parent process */ - pid = fork(); - if (pid < 0) { - exit(1); - /* if we got a good PID, then we can exit the parent process */ - } else if (pid > 0) { - exit(0); - } - - /* ↓ at this point we are executing as the child process ↓ */ - - /* change the file mode mask */ - umask(027); - - /* create a new SID for the child process */ - sid = setsid(); - if (sid < 0) { - exit(1); - } - - /* Change the current working directory. This prevents the current - * directory from being locked; hence not being able to remove it. - * /tmp seems to be the best place where to create temporary files ;c) - */ - if (chdir("/tmp") < 0) { - exit(1); - } - - /* say loudly what PID we got */ - printf("The Tuxanci game server started with the PID %d\n", sid); - - /* write the PID to the PID file */ - pid_file = fopen(public_server_get_setting("PID_FILE", "--pid-file", "/var/run/tuxanci-server.pid"), "w"); - fprintf(pid_file, "%d\n", sid); - fclose(pid_file); - - /* redirect standard files to /dev/null */ - freopen("/dev/null", "r", stdin); - freopen("/dev/null", "w", stdout); - freopen("/dev/null", "w", stderr); -#endif /* __WIN32__ */ -} - -/** - * Tell MasterServer that we want to be propagated - */ -static int public_server_register() -{ -#ifndef __WIN32__ - int s; -#else /* __WIN32__ */ - SOCKET s; -#endif /* __WIN32__ */ - - struct sockaddr_in server; - char *master_server_ip; - - master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN); - - if (master_server_ip == NULL) { - warning("Did not obtained IP of the MasterServer"); - return -1; - } - - server.sin_family = AF_INET; - server.sin_port = htons(NET_MASTER_PORT); - server.sin_addr.s_addr = inet_addr(master_server_ip); - - free(master_server_ip); - - if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - warning("Unable to open socket for connection to MasterServer"); - return -1; - } - - /* connect to MasterServer */ - if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) { - warning("Unable to estabilish connection to MasterServer"); - return -1; - } - - typedef struct { - unsigned char cmd; - unsigned port; - unsigned ip; - } __PACKED__ register_head; - - register_head *head = (register_head *) malloc(sizeof(register_head)); - - head->cmd = 'p'; - head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800")); - head->ip = 0; /* TODO */ - - /* send request for server list */ - int r = send(s, head, sizeof(register_head), 0); - - free(head); - -#ifndef __WIN32__ - close(s); -#else /* __WIN32__ */ - closesocket(s); -#endif /* __WIN32__ */ - - if (r == -1) { - return -1; - } else { - return 0; - } -} - -/** - * Tell MasterServer that we are shutting down - */ -static int public_server_unregister() -{ -#ifndef __WIN32__ - int s; -#else /* __WIN32__ */ - SOCKET s; -#endif /* __WIN32__ */ - - struct sockaddr_in server; - char *master_server_ip; - - master_server_ip = dns_resolv(NET_MASTER_SERVER_DOMAIN); - - if (master_server_ip == NULL) { - warning("Did not obtained IP of the MasterServer"); - return -1; - } - - server.sin_family = AF_INET; - server.sin_port = htons(NET_MASTER_PORT); - server.sin_addr.s_addr = inet_addr(master_server_ip); - - free(master_server_ip); - - if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - warning("Unable to open socket for connection to MasterServer"); - return -1; - } - - /* connect to MasterServer */ - if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0) { - warning("Unable to estabilish connection to MasterServer"); - return -1; - } - - typedef struct { - unsigned char cmd; - unsigned port; - unsigned ip; - } __PACKED__ register_head; - - register_head *head = (register_head *) malloc(sizeof(register_head)); - - head->cmd = 'u'; - head->port = atoi(public_server_get_setting("PORT4", "--port4", "6800")); - head->ip = 0; /* TODO */ - - /* send request for server list */ - int r = send(s, head, sizeof(register_head), 0); - - free(head); - -#ifndef __WIN32__ - close(s); -#else /* __WIN32__ */ - closesocket(s); -#endif /* __WIN32__ */ - - if (r == -1) { - return -1; - } else { - return 0; - } -} - -static int public_server_initNetwork() -{ - char ip4[STR_IP_SIZE]; - char ip6[STR_IP_SIZE]; - char *p_ip4, *p_ip6; - int port; - int ret; - - ret = -1; - - strcpy(ip4, public_server_get_setting("IP4", "--ip4", "none")); - strcpy(ip6, public_server_get_setting("IP6", "--ip6", "none")); - - p_ip4 = ip4; - - if (strcmp(ip4, "none") == 0 || strcmp(ip6, "::") == 0) { - p_ip4 = NULL; - } - - p_ip6 = ip6; - - if (strcmp(ip6, "none") == 0) { - p_ip6 = NULL; - } - - port = atoi(public_server_get_setting("PORT", "--port", "6800")); - - ret = net_multiplayer_init_for_game_server(p_ip4, p_ip6, port); - - return ret; -} - -static void load_arena() -{ - choice_arenaFile = arena_file_get_file_format_net_name(public_server_get_setting("ARENA", "--arena", "FAGN")); - - if (choice_arenaFile == NULL) { - error("Unable to load arena [%s]", public_server_get_setting("ARENA", "--arena", "FAGN")); - /* TODO: Why not to log it? */ - exit(-1); - } - - arena = arena_file_get_arena(choice_arenaFile); - - arena_set_current(arena); -} - -int public_server_init() -{ - int ret; - int i; - - id_init_list(); - module_init(); - arena_file_init(); - tux_init(); - item_init(); - shot_init(); - server_configFile_init(); - - ret = log_init(public_server_get_setting("LOG_FILE", "--log-file", "/tmp/tuxanci-server.log")); - - if (ret < 0) { - /* Error message has been already printed */ - return -1; - } - - high_score_init(public_server_get_setting("SCORE_FILE", "--score-file", "/tmp/highscore.txt")); - - load_arena(); - - for (i = 0; i < atoi(public_server_get_setting("ITEM", "--item", "10")); i++) { - item_add_new_item(arena->spaceItem, ID_UNKNOWN); - } - - isSignalEnd = FALSE; - - ret = public_server_initNetwork(); - - if (ret < 0) { - error("Unable to initialize network socket"); - /* TODO: Why not to log it? */ - return -1; - } - - server_set_max_clients(atoi(public_server_get_setting("MAX_CLIENTS", "--max-clients", "32"))); - - if (atoi(public_server_get_setting("LAN_ONLY", "--lan-only", "0")) == 0) { - if (public_server_register() < 0) { - error("Unable to contact MasterServer"); - /* TODO: Why not to log it? */ - } - } else { - debug("Starting LAN-only server"); - } - - return 0; -} - -arenaFile_t *choice_arena_get() -{ - return choice_arenaFile; -} - -void public_server_event() -{ - static my_time_t lastActive = 0; - my_time_t interval; - - net_multiplayer_event(); - - if (isSignalEnd == TRUE) { - public_server_quit(); - } - - if (lastActive == 0) { - lastActive = timer_get_current_time(); - } - - interval = timer_get_current_time() - lastActive; - - if (interval < 50) { - return; - } - - lastActive = timer_get_current_time(); - - arena_event(arena); -} - -void my_handler_quit(int n) -{ - debug("Received signal %d", n); - /* TODO: Why not to log it? */ - - isSignalEnd = TRUE; - - public_server_quit(); -} - -void public_server_quit() -{ - debug("Shutting down the Tuxanci game server"); - /* TODO: Why not to log it? */ - - if (atoi(public_server_get_setting("LAN_ONLY", "--lan-only", "0")) == 0) { - if (public_server_unregister() < 0) { - error("Unable to contact MasterServer"); - /* TODO: Why not to log it? */ - } - } - - net_multiplayer_quit(); - arena_destroy(arena); - - tux_quit(); - item_quiy(); - shot_quit(); - - /* remove the PID file */ - remove(public_server_get_setting("PID_FILE", "--pid-file", "/var/run/tuxanci-server.pid")); - - arena_file_quit(); - server_configFile_quit(); - module_quit(); - id_quit_list(); - high_score_quit(); - log_quit(); - - exit(0); -} - -int public_server_start() -{ -#ifndef __WIN32__ - signal(SIGINT, my_handler_quit); - signal(SIGTERM, my_handler_quit); - signal(SIGQUIT, my_handler_quit); -#endif /* __WIN32__ */ - if (public_server_init() < 0) { - public_server_quit(); - return -1; - } - - char *s = public_server_get_setting("DAEMON", "--daemon", "0"); - - if (atoi(s)) { - daemonize(); - } - - for (;;) { - public_server_event(); - } - - return 0; -} diff --git a/src/server/publicServer.h b/src/server/publicServer.h deleted file mode 100644 index 59c029e..0000000 --- a/src/server/publicServer.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef PUBLIC_SERVER_H -#define PUBLIC_SERVER_H - -#include "arena.h" -#include "arenaFile.h" - -#define WORLD_COUNT_ROUND_UNLIMITED -1 - -extern char *public_server_get_setting(char *env, char *param, char *default_val); -extern void world_inc_round(); -extern int public_server_init(); -extern arenaFile_t *choice_arena_get(); -extern void eventPublicServer(); -extern void my_handler_quit(int n); -extern void public_server_quit(); -extern int public_server_start(); - -#endif /* PUBLIC_SERVER_H */ diff --git a/src/server/serverConfigFile.c b/src/server/serverConfigFile.c deleted file mode 100644 index d872827..0000000 --- a/src/server/serverConfigFile.c +++ /dev/null @@ -1,94 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#include "main.h" -#include "list.h" -#include "textFile.h" -#include "serverConfigFile.h" - -static textFile_t *serverTextFile; - -static void prepareConfigFile(textFile_t *ts) -{ - int i, j; - - for (i = 0; i < serverTextFile->text->count; i++) { - char *line; - int len; - - line = (char *) (serverTextFile->text->list[i]); - len = strlen(line); - - for (j = 0; j < len; j++) { - if (line[j] == ' ') { /* [TAB] */ - line[j] = ' '; - } - } - } -} - -void server_configFile_init() -{ - char *configFile; - - configFile = getParamElse("--config-file", SERVER_CONFIG); - debug("Loading configuration file [%s]", getParamElse("--config-file", SERVER_CONFIG)); - - serverTextFile = text_file_load(configFile); - - if (serverTextFile == NULL) { - warning("Unable to load config file - using defaults"); - return; - } - - prepareConfigFile(serverTextFile); -} - -static char *findValue(char *s) -{ - int i; - int len; - - len = strlen(s); - - for (i = 0; i < len - 1; i++) { - if (s[i] == ' ' && s[i + 1] != ' ') { - return s + i + 1; - } - } - - return NULL; -} - -char *server_configFile_get_value(char *env, char *s) -{ - int i; - int len; - - if (serverTextFile == NULL) { - return s; - } - - len = strlen(env); - - for (i = 0; i < serverTextFile->text->count; i++) { - char *line; - - line = (char *) (serverTextFile->text->list[i]); - - if (strncmp(line, env, len) == 0) { - return findValue(line); - } - } - - return s; -} - -void server_configFile_quit() -{ - if (serverTextFile != NULL) { - text_file_destroy(serverTextFile); - } -} diff --git a/src/server/serverConfigFile.h b/src/server/serverConfigFile.h deleted file mode 100644 index b17b90f..0000000 --- a/src/server/serverConfigFile.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SERVER_CONFIG_FILE_H -#define SERVER_CONFIG_FILE_H - -extern void server_configFile_init(); -extern char *server_configFile_get_value(char *env, char *s); -extern void server_configFile_quit(); - -#endif /* SERVER_CONFIG_FILE_H */ diff --git a/src/ta_al.c b/src/ta_al.c new file mode 100644 index 0000000..5d6076b --- /dev/null +++ b/src/ta_al.c @@ -0,0 +1,30 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifdef ENABLE_AUDIO + +#include <AL/al.h> +#include <AL/alc.h> +#include "ta_sdl.h" +#include "logging.h" +#include "main.h" + +// unused for now + +#endif // ENABLE_AUDIO
\ No newline at end of file diff --git a/src/ta_alc.c b/src/ta_alc.c new file mode 100644 index 0000000..a1dc06a --- /dev/null +++ b/src/ta_alc.c @@ -0,0 +1,70 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifdef ENABLE_AUDIO + +#include <AL/al.h> +#include <AL/alc.h> +#include "ta_sdl.h" +#include "logging.h" +#include "main.h" + +ALCdevice *ta_alc_open_device(void) { + ALCchar *devicename = NULL; // default + + ta_log("Opening AL device"); + ALCdevice *device = alcOpenDevice(devicename); + if (device) { + return device; + } else { + SDL_Log("AL: Failed to open device"); + return NULL; + } +} + +ALCcontext *ta_alc_create_context(ALCdevice *device, ALCint* attrlist) { + ta_log("Creating AL context"); + ALCcontext *context = alcCreateContext(device, attrlist); + if (context) { + return context; + } else { + SDL_Log("AL: Failed to create context"); + return NULL; + } +} + +void ta_alc_make_context_current(ALCcontext *context) { + ta_log("Making AL context current"); + ALCboolean status = alcMakeContextCurrent(context); + if (status == ALC_FALSE) { + SDL_Log("AL: Failed to make context current"); + ta_main_exit(1); + } +} + +void ta_alc_close_device(ALCdevice *device) { + ta_log("Closing AL device"); + ALCboolean status = alcCloseDevice(device); + if (status == ALC_FALSE) { + SDL_Log("AL: Failed to close device"); + ta_main_exit(1); + } +} + +#endif // ENABLE_AUDIO
\ No newline at end of file diff --git a/src/ta_openmpt.c b/src/ta_openmpt.c new file mode 100644 index 0000000..987f384 --- /dev/null +++ b/src/ta_openmpt.c @@ -0,0 +1,61 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifdef ENABLE_AUDIO + +#include <stdbool.h> +#include <libopenmpt/libopenmpt.h> +#include "logging.h" +#include "ta_sdl.h" +#include "main.h" + +openmpt_module *ta_openmpt_load_module(unsigned char *start, size_t size) { + const char *error = NULL; + + ta_log("Loading OpenMPT module from memory"); + openmpt_module *module = openmpt_module_create_from_memory2( + start, + size, + NULL, + NULL, + NULL, + NULL, + NULL, + &error, + NULL + ); + if (module) { + return module; + } else { + SDL_Log("OpenMPT: %s", error); + ta_main_exit(1); + return NULL; + } +} + +void ta_openmpt_set_module_repeat_count(openmpt_module *module, int count) { + ta_log("Setting OpenMPT module repeat count"); + bool success = openmpt_module_set_repeat_count(module, count); + if (!success) { + SDL_Log("Failed to set OpenMPT module repeat count"); + ta_main_exit(1); + } +} + +#endif // ENABLE_AUDIO
\ No newline at end of file diff --git a/src/ta_sdl.c b/src/ta_sdl.c new file mode 100644 index 0000000..d1519df --- /dev/null +++ b/src/ta_sdl.c @@ -0,0 +1,233 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdbool.h> +#include <stdlib.h> +#include "ta_sdl.h" +#include "logging.h" +#include "main.h" + +static void ta_sdl_handle_error(void) { + const char *error = SDL_GetError(); + SDL_Log("SDL: %s\n", error); + + ta_main_exit(1); +} + +void ta_sdl_init(SDL_InitFlags flags) { + // TODO: Make it log what subsystem + ta_log("Initializing a SDL subsystem"); + bool success = SDL_Init(flags); + if (!success) { + ta_sdl_handle_error(); + } +} + +void ta_sdl_shutdown(void) { + ta_log("Exiting SDL"); + SDL_Quit(); +} + +SDL_Window *ta_sdl_create_window(const char *title, int width, int height) { + SDL_WindowFlags flags = { 0 }; + + ta_log("Initializing SDL window"); + SDL_Window *window = SDL_CreateWindow(title, width, height, flags); + if (window) { + return window; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_IOStream *ta_sdl_io_from_file(const char *file, const char *mode) { + ta_log("Opening %s as %s", file, mode); + + SDL_IOStream *io = SDL_IOFromFile(file, mode); + if (io) { + return io; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_IOStream *ta_sdl_io_from_mem(void *mem, size_t size) { + ta_log("Loading IO from memory"); + SDL_IOStream *io = SDL_IOFromMem(mem, size); + if (io) { + return io; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +void ta_sdl_get_window_size(SDL_Window *window, int *width, int *height) { + bool success = SDL_GetWindowSize(window, width, height); + if (!success) { + ta_sdl_handle_error(); + } +} + +SDL_GPUDevice *ta_sdl_create_gpu_device(void) { + ta_log("Creating SDL_gpu device"); + SDL_GPUDevice *device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL); + if (device) { + return device; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +void ta_sdl_claim_window_for_gpu_device(SDL_GPUDevice *device, SDL_Window *window) { + ta_log("Claiming SDL window for SDL_gpu device"); + bool success = SDL_ClaimWindowForGPUDevice(device, window); + if (!success) { + ta_sdl_handle_error(); + } +} + +SDL_GPUShader *ta_sdl_create_gpu_shader(SDL_GPUDevice *device, const SDL_GPUShaderCreateInfo *createinfo) { + ta_log("Creating a SDL_gpu shader"); + SDL_GPUShader *shader = SDL_CreateGPUShader(device, createinfo); + if (shader) { + return shader; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUGraphicsPipeline *ta_sdl_create_gpu_graphics_pipeline(SDL_GPUDevice *device, const SDL_GPUGraphicsPipelineCreateInfo *createinfo) { + ta_log("Creating a SDL_gpu graphics pipeline"); + SDL_GPUGraphicsPipeline *graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, createinfo); + if (graphics_pipeline) { + return graphics_pipeline; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUTexture *ta_sdl_create_gpu_texture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *createinfo) { + SDL_GPUTexture *texture = SDL_CreateGPUTexture(device, createinfo); + if (texture) { + return texture; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUSampler *ta_sdl_create_gpu_sampler(SDL_GPUDevice *device, const SDL_GPUSamplerCreateInfo *createinfo) { + SDL_GPUSampler *sampler = SDL_CreateGPUSampler(device, createinfo); + if (sampler) { + return sampler; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUTransferBuffer *ta_sdl_create_gpu_transfer_buffer(SDL_GPUDevice *device, const SDL_GPUTransferBufferCreateInfo *createinfo) { + SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, createinfo); + if (transfer_buffer) { + return transfer_buffer; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +void *ta_sdl_map_gpu_transfer_buffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *transfer_buffer, bool cycle) { + void *mapped = SDL_MapGPUTransferBuffer(device, transfer_buffer, cycle); + if (mapped) { + return mapped; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUCommandBuffer *ta_sdl_acquire_gpu_command_buffer(SDL_GPUDevice *device) { + SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device); + if (command_buffer) { + return command_buffer; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPUCopyPass *ta_sdl_begin_gpu_copy_pass(SDL_GPUCommandBuffer *command_buffer) { + SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer); + if (copy_pass) { + return copy_pass; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +SDL_GPURenderPass *ta_sdl_begin_gpu_render_pass(SDL_GPUCommandBuffer *command_buffer, const SDL_GPUColorTargetInfo *color_target_infos, Uint32 num_color_targets, const SDL_GPUDepthStencilTargetInfo *depth_stencil_target_info) { + SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(command_buffer, color_target_infos, num_color_targets, depth_stencil_target_info); + if (render_pass) { + return render_pass; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +void ta_sdl_wait_and_acquire_gpu_swapchain_texture(SDL_GPUCommandBuffer *commandbuffer, SDL_Window *window, SDL_GPUTexture **swapchain_texture) { + Uint32 *swapchain_texture_width = NULL; + Uint32 *swapchain_texture_height = NULL; + + bool success = SDL_WaitAndAcquireGPUSwapchainTexture(commandbuffer, window, swapchain_texture, swapchain_texture_width, swapchain_texture_height); + if (!success) { + ta_sdl_handle_error(); + } +} + +void ta_sdl_submit_gpu_command_buffer(SDL_GPUCommandBuffer *command_buffer) { + bool success = SDL_SubmitGPUCommandBuffer(command_buffer); + if (!success) { + ta_sdl_handle_error(); + } +} + +SDL_Surface *ta_sdl_convert_surface(SDL_Surface *surface, SDL_PixelFormat format) { + SDL_Surface *converted_surface = SDL_ConvertSurface(surface, format); + if (converted_surface) { + return converted_surface; + } else { + ta_sdl_handle_error(); + return NULL; + } +} + +void ta_sdl_set_window_fullscreen(SDL_Window *window, bool fullscreen) { + bool success = SDL_SetWindowFullscreen(window, fullscreen); + if (!success) { + ta_sdl_handle_error(); + } +}
\ No newline at end of file diff --git a/src/ta_sdl_ttf.c b/src/ta_sdl_ttf.c new file mode 100644 index 0000000..4a37a4f --- /dev/null +++ b/src/ta_sdl_ttf.c @@ -0,0 +1,72 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ta_sdl_ttf.h" +#include "main.h" +#include "logging.h" + +static void ta_sdl_ttf_handle_error(void) { + const char *error = SDL_GetError(); + SDL_Log("SDL_ttf: %s\n", error); + + ta_main_exit(1); +} + +void ta_sdl_ttf_init(void) { + bool success = TTF_Init(); + if (!success) { + ta_sdl_ttf_handle_error(); + } +} + +void ta_sdl_ttf_shutdown(void) { + ta_log("Exiting SDL_ttf"); + TTF_Quit(); +} + +TTF_Font *ta_sdl_ttf_open_font(SDL_IOStream *src) { + bool closeio = true; + float ptsize = 1.0f; + + ta_log("Loading font"); + TTF_Font *font = TTF_OpenFontIO(src, closeio, ptsize); + if (font) { + return font; + } else { + ta_sdl_ttf_handle_error(); + return NULL; + } +} + +void ta_sdl_ttf_set_font_size(TTF_Font *font, float ptsize) { + bool success = TTF_SetFontSize(font, ptsize); + if (!success) { + ta_sdl_ttf_handle_error(); + } +} + +SDL_Surface *ta_sdl_ttf_render_text_blended(TTF_Font *font, const char *text, size_t length, SDL_Color color) { + SDL_Surface *surface = TTF_RenderText_Blended(font, text, length, color); + if (surface) { + return surface; + } else { + ta_sdl_ttf_handle_error(); + return NULL; + } +}
\ No newline at end of file diff --git a/src/teapot.c b/src/teapot.c new file mode 100644 index 0000000..751eb55 --- /dev/null +++ b/src/teapot.c @@ -0,0 +1,70 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "files.h" +#include "gpu.h" +#include "obj_model.h" +#include "teapot.h" +#include "window.h" + +static ta_obj_model ta_teapot_model; +static ta_obj_model ta_dounut_model; + +void ta_teapot_init(void) { + ta_obj_model_init( + &ta_teapot_model, + ta_gpu_device, + file_data_models_teapot_obj_start, + file_data_models_teapot_obj_size, + ta_gpu_swapchain_format, + SDL_GPU_TEXTUREFORMAT_D32_FLOAT, + ta_gpu_sample_count + ); + ta_teapot_model.position[1] = 1.0f; + + ta_obj_model_init( + &ta_dounut_model, + ta_gpu_device, + file_data_models_dounut_obj_start, + file_data_models_dounut_obj_size, + ta_gpu_swapchain_format, + SDL_GPU_TEXTUREFORMAT_D32_FLOAT, + ta_gpu_sample_count + ); + ta_dounut_model.position[1] = -1.0f; + ta_dounut_model.rotation[0] = -1.0f; + +} + +void ta_teapot_upload(SDL_GPUCommandBuffer *command_buffer) { + ta_obj_model_upload(&ta_teapot_model, command_buffer, ta_gpu_device); + ta_obj_model_upload(&ta_dounut_model, command_buffer, ta_gpu_device); +} + +void ta_teapot_render(SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass) { + ta_teapot_model.rotation[1] = (float)SDL_GetTicks() * 0.01f; + ta_obj_model_render(&ta_teapot_model, command_buffer, render_pass, ta_window_width, ta_window_height); + ta_dounut_model.rotation[1] = (float)SDL_GetTicks() * 0.01f; + ta_obj_model_render(&ta_dounut_model, command_buffer, render_pass, ta_window_width, ta_window_height); +} + +void ta_teapot_shutdown(void) { + ta_obj_model_destroy(&ta_teapot_model, ta_gpu_device); + ta_obj_model_destroy(&ta_dounut_model, ta_gpu_device); +} diff --git a/src/text.c b/src/text.c new file mode 100644 index 0000000..6cff2b4 --- /dev/null +++ b/src/text.c @@ -0,0 +1,205 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "files.h" +#include "logging.h" +#include "main.h" +#include "ta_sdl_ttf.h" +#include "ta_sdl.h" +#include "gpu.h" +#include "window.h" +#include "color.h" + +static TTF_Font *ta_text_font; +SDL_Surface *ta_text_surface; +int ta_text_current_x; +int ta_text_current_y; + +static void ta_text_upload_surface(SDL_GPUCommandBuffer *command_buffer, SDL_Surface *text_surface, SDL_GPUTexture **texture, Uint32 *texture_width, Uint32 *texture_height) { + if (!text_surface) { + return; + } + + SDL_Surface *surface = ta_sdl_convert_surface(text_surface, SDL_PIXELFORMAT_RGBA32); + + Uint32 width = (Uint32)surface->w; + Uint32 height = (Uint32)surface->h; + if (!*texture || width != *texture_width || height != *texture_height) { + SDL_ReleaseGPUTexture(ta_gpu_device, *texture); + SDL_GPUTextureCreateInfo texture_info = { + .type = SDL_GPU_TEXTURETYPE_2D, + .format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, + .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER, + .width = width, + .height = height, + .layer_count_or_depth = 1, + .num_levels = 1, + }; + *texture = ta_sdl_create_gpu_texture(ta_gpu_device, &texture_info); + *texture_width = width; + *texture_height = height; + } + + SDL_GPUTransferBufferCreateInfo transfer_info = { + .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, + .size = width * height * 4, + }; + SDL_GPUTransferBuffer *transfer_buffer = ta_sdl_create_gpu_transfer_buffer(ta_gpu_device, &transfer_info); + void *mapped = ta_sdl_map_gpu_transfer_buffer(ta_gpu_device, transfer_buffer, false); + for (Uint32 row = 0; row < height; row++) { + memcpy( + (Uint8 *)mapped + row * width * 4, + (Uint8 *)surface->pixels + row * (Uint32)surface->pitch, + width * 4 + ); + } + SDL_UnmapGPUTransferBuffer(ta_gpu_device, transfer_buffer); + + SDL_GPUTextureTransferInfo source = { + .transfer_buffer = transfer_buffer, + .pixels_per_row = width, + .rows_per_layer = height, + }; + SDL_GPUTextureRegion destination = { + .texture = *texture, + .w = width, + .h = height, + .d = 1, + }; + SDL_GPUCopyPass *copy_pass = ta_sdl_begin_gpu_copy_pass(command_buffer); + SDL_UploadToGPUTexture(copy_pass, &source, &destination, true); + SDL_EndGPUCopyPass(copy_pass); + SDL_ReleaseGPUTransferBuffer(ta_gpu_device, transfer_buffer); + + SDL_DestroySurface(surface); +} + +void ta_gpu_upload_text(SDL_GPUCommandBuffer *command_buffer) { + ta_text_upload_surface(command_buffer, ta_text_surface, &ta_gpu_text_texture, &ta_gpu_text_texture_width, &ta_gpu_text_texture_height); +} + +static void ta_text_render_surface(SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass, SDL_Surface *text_surface, SDL_GPUTexture *texture, Uint32 texture_width, Uint32 texture_height, int x, int y) { + if (!text_surface) { + return; + } + + float clip_rect[4] = { + 2.0f * (float) x / (float)ta_window_width - 1.0f, + 1.0f - 2.0f * (float)(y + (int)texture_height) / (float)ta_window_height, + 2.0f * (float)(x + (int)texture_width) / (float)ta_window_width - 1.0f, + 1.0f - 2.0f * (float) y / (float)ta_window_height, + }; + SDL_PushGPUVertexUniformData(command_buffer, 0, clip_rect, sizeof(clip_rect)); + SDL_BindGPUGraphicsPipeline(render_pass, ta_gpu_text_pipeline); + SDL_GPUTextureSamplerBinding binding = { + .texture = texture, + .sampler = ta_gpu_text_sampler, + }; + SDL_BindGPUFragmentSamplers(render_pass, 0, &binding, 1); + SDL_DrawGPUPrimitives(render_pass, 6, 1, 0, 0); +} + +void ta_text_render(SDL_GPUCommandBuffer *command_buffer, SDL_GPURenderPass *render_pass) { + ta_text_render_surface(command_buffer, render_pass, ta_text_surface, ta_gpu_text_texture, ta_gpu_text_texture_width, ta_gpu_text_texture_height, ta_text_current_x, ta_text_current_y); +} + +void ta_text_init(void) { + ta_sdl_ttf_init(); + + SDL_IOStream *font_io = ta_sdl_io_from_mem( + file_data_fonts_DejaVuSans_ttf_start, + file_data_fonts_DejaVuSans_ttf_size + ); + + ta_text_font = ta_sdl_ttf_open_font(font_io); +} + +static void ta_text_draw_surface(SDL_Surface **target, float size, int red, int green, int blue, int alpha, const char *format, va_list arguments) { + if (!ta_text_font || !format || size <= 0.0f) { + return; + } + + va_list length_arguments; + va_copy(length_arguments, arguments); + int length = vsnprintf(NULL, 0, format, length_arguments); + va_end(length_arguments); + if (length < 0) { + return; + } + + char *text = malloc((size_t)length + 1); + if (!text) { + return; + } + + va_list text_arguments; + va_copy(text_arguments, arguments); + vsnprintf(text, (size_t)length + 1, format, text_arguments); + va_end(text_arguments); + + ta_sdl_ttf_set_font_size(ta_text_font, size); + + SDL_Color color = { + red, + green, + blue, + alpha + }; + SDL_Surface *surface = ta_sdl_ttf_render_text_blended(ta_text_font, text, 0, color); + free(text); + + SDL_DestroySurface(*target); + *target = surface; +} + +void ta_text_draw(int x, int y, float size, ta_color color, const char *format, ...) { + int red = color.red; + int green = color.green; + int blue = color.blue; + int alpha = color.alpha; + + va_list arguments; + va_start(arguments, format); + + ta_text_draw_surface(&ta_text_surface, size, red, green, blue, alpha, format, arguments); + + va_end(arguments); + ta_text_current_x = x; + ta_text_current_y = y; +} + +void ta_text_clear(void) { + SDL_DestroySurface(ta_text_surface); + ta_text_surface = NULL; +} + +void ta_text_shutdown(void) { + ta_text_clear(); + + if (ta_text_font) { + TTF_CloseFont(ta_text_font); + ta_text_font = NULL; + } + + TTF_Quit(); +} diff --git a/src/tuxanci.rc b/src/tuxanci.rc deleted file mode 100644 index 1198660..0000000 --- a/src/tuxanci.rc +++ /dev/null @@ -1 +0,0 @@ -1 ICON "../data/tuxanci.svg" diff --git a/src/widget/CMakeLists.txt b/src/widget/CMakeLists.txt deleted file mode 100644 index 2e93cef..0000000 --- a/src/widget/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -############################################################################### -# tuxanci/src/*/CMakeLists.txt -############################################################################### -# Generate source_code files from .c files we find in directory -############################################################################### -MacroAddSources() -############################################################################### diff --git a/src/widget/widget.c b/src/widget/widget.c deleted file mode 100644 index 2c0651b..0000000 --- a/src/widget/widget.c +++ /dev/null @@ -1,102 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" - -#include "widget.h" -#include "widget_label.h" -#include "widget_button.h" -#include "widget_buttonimage.h" -#include "widget_textfield.h" -#include "widget_catchkey.h" -#include "widget_check.h" -#include "widget_choicegroup.h" -#include "widget_image.h" -#include "widget_select.h" -#include "widget_statusbar.h" - -widget_t *widget_new(int type, int x, int y, int w, int h, void *private_data) -{ - widget_t *new; - - new = malloc(sizeof(widget_t)); - new->type = type; - new->x = x; - new->y = y; - new->w = w; - new->h = h; - new->private_data = private_data; - - return new; -} - -void widget_set_location(widget_t *p, int x, int y) -{ - p->x = x; - p->y = y; -} - -void widget_get_location(widget_t *p, int *x, int *y) -{ - if (x != NULL) { - *x = p->x; - } - - if (y != NULL) { - *y = p->y; - } -} - -void widget_get_size(widget_t *p, int w, int h) -{ - p->w = w; - p->h = h; -} - -void widget_set_size(widget_t *p, int *w, int *h) -{ - if (w != NULL) { - *w = p->w; - } - - if (h != NULL) { - *h = p->h; - } -} - -void widget_draw(widget_t *p) -{ - switch (p->type) { - case WIDGET_TYPE_LABEL: label_draw(p); break; - case WIDGET_TYPE_BUTTON: button_draw(p); break; - case WIDGET_TYPE_BUTTONIMAGE: button_image_draw(p); break; - case WIDGET_TYPE_TEXTFILED: text_field_draw(p); break; - case WIDGET_TYPE_CATCHKEY: catch_key_draw(p); break; - case WIDGET_TYPE_CHECK: check_draw(p); break; - case WIDGET_TYPE_CHOICE: choice_group_draw(p); break; - case WIDGET_TYPE_IMAGE: wid_image_draw(p); break; - case WIDGET_TYPE_SELECT: select_draw(p); break; - case WIDGET_TYPE_STATUSBAR: statusbar_draw(p); break; - } -} - -void widget_event(widget_t *p) -{ - switch (p->type) { - case WIDGET_TYPE_LABEL: label_event(p); break; - case WIDGET_TYPE_BUTTON: button_event(p); break; - case WIDGET_TYPE_BUTTONIMAGE: button_image_event(p); break; - case WIDGET_TYPE_TEXTFILED: text_field_event(p); break; - case WIDGET_TYPE_CATCHKEY: catch_key_event(p); break; - case WIDGET_TYPE_CHECK: check_event(p); break; - case WIDGET_TYPE_CHOICE: choice_group_event(p); break; - case WIDGET_TYPE_IMAGE: wid_image_event(p); break; - case WIDGET_TYPE_SELECT: select_event(p); break; - case WIDGET_TYPE_STATUSBAR: statusbar_event(p); break; - } -} - -void widget_destroy(widget_t *p) -{ - free(p); -} diff --git a/src/widget/widget.h b/src/widget/widget.h deleted file mode 100644 index b5898c2..0000000 --- a/src/widget/widget.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef WIDGET_H -#define WIDGET_H - -#include "main.h" - -#define WIDGET_TYPE_LABEL 1 -#define WIDGET_TYPE_BUTTON 2 -#define WIDGET_TYPE_BUTTONIMAGE 3 -#define WIDGET_TYPE_TEXTFILED 4 -#define WIDGET_TYPE_CATCHKEY 5 -#define WIDGET_TYPE_CHECK 6 -#define WIDGET_TYPE_CHOICE 7 -#define WIDGET_TYPE_IMAGE 8 -#define WIDGET_TYPE_SELECT 9 -#define WIDGET_TYPE_STATUSBAR 10 -#define WIDGET_TYPE_CONTAINER 11 - -#define WIDGET_NONE_VALUE -1 - -typedef struct widget_struct { - int type; - int x, y; - int w, h; - void *private_data; -} widget_t; - -extern widget_t *widget_new(int type, int x, int y, int w, int h, void *private_data); -extern void widget_set_location(widget_t *p, int x, int y); -extern void widget_get_location(widget_t *p, int *x, int *y); -extern void widget_get_size(widget_t *p, int w, int h); -extern void widget_set_size(widget_t *p, int *w, int *h); -extern void widget_draw(widget_t *p); -extern void widget_event(widget_t *p); -extern void widget_destroy(widget_t *p); - -#endif /* WIDGET_H */ diff --git a/src/widget/widget_button.c b/src/widget/widget_button.c deleted file mode 100644 index 9d80294..0000000 --- a/src/widget/widget_button.c +++ /dev/null @@ -1,101 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" - -#include "interface.h" -#include "image.h" -#include "font.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_button.h" - -widget_t *button_new(char *text, int x, int y, void (*fce_event) (void *)) -{ - widget_button_t *new; - - new = malloc(sizeof(widget_button_t)); - new->text = strdup(text); - new->fce_event = fce_event; - font_text_size(text, &(new->w), &(new->h)); - - return widget_new(WIDGET_TYPE_BUTTON, x, y, - WIDGET_BUTTON_WIDTH, WIDGET_BUTTON_HEIGHT, new); -} - -void button_draw(widget_t *widget) -{ - widget_button_t *p; - static image_t *g_button0 = NULL; - static image_t *g_button1 = NULL; - int x, y; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTON); - - p = (widget_button_t *) widget->private_data; - interface_get_mouse_position(&x, &y); - - if (g_button0 == NULL) { - g_button0 = image_add("button0.png", IMAGE_ALPHA, "button0", IMAGE_GROUP_BASE); - } - - if (g_button1 == NULL) { - g_button1 = image_add("button1.png", IMAGE_ALPHA, "button1", IMAGE_GROUP_BASE); - } - - if (mouse_buffer_is_on_area(widget->x, widget->y, WIDGET_BUTTON_WIDTH, - WIDGET_BUTTON_HEIGHT, MOUSE_BUF_MOTION)) { - image_draw(g_button1, widget->x, widget->y, 0, 0, - g_button0->w, g_button0->h); - } else { - image_draw(g_button0, widget->x, widget->y, 0, 0, - g_button0->w, g_button0->h); - } - - /*font_draw(p->text, p->x+WIDGET_BUTTON_WIDTH/2-p->w/2, p->y+p->h/2, COLOR_WHITE);*/ - font_draw(p->text, widget->x + WIDGET_BUTTON_WIDTH / 2 - p->w / 2, - widget->y + WIDGET_BUTTON_HEIGHT / 2 - p->h / 2, - COLOR_WHITE); -} - -void button_event(widget_t *widget) -{ - widget_button_t *p; - static int my_time = 0; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTON); - - p = (widget_button_t *) widget->private_data; - - if (my_time > 0) { - my_time--; - return; - } - - if (mouse_buffer_is_on_area(widget->x, widget->y, WIDGET_BUTTON_WIDTH, - WIDGET_BUTTON_HEIGHT, MOUSE_BUF_CLICK)) { - my_time = WIDGET_BUTTON_TIME; - - debug("Caught some button event"); - - p->fce_event(widget); - } -} - -void button_destroy(widget_t *widget) -{ - widget_button_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTON); - - p = (widget_button_t *) widget->private_data; - - free(p->text); - free(p); - - widget_destroy(widget); -} diff --git a/src/widget/widget_button.h b/src/widget/widget_button.h deleted file mode 100644 index 2ec4ae3..0000000 --- a/src/widget/widget_button.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef WIDGET_BUTTON_H -#define WIDGET_BUTTON_H - -#include "main.h" -#include "widget.h" - -#define WIDGET_BUTTON_TIME 10 - -#define WIDGET_BUTTON_WIDTH 125 -#define WIDGET_BUTTON_HEIGHT 36 - -typedef struct widget_button { - char *text; - int w, h; - void (*fce_event) (void *); -} widget_button_t; - -extern widget_t *button_new(char *text, int x, int y, void (*fce_event) (void *)); -extern void button_draw(widget_t *widget); -extern void button_event(widget_t *widget); -extern void button_destroy(widget_t *widget); - -#endif /* WIDGET_BUTTON_H */ diff --git a/src/widget/widget_buttonimage.c b/src/widget/widget_buttonimage.c deleted file mode 100644 index 134d34e..0000000 --- a/src/widget/widget_buttonimage.c +++ /dev/null @@ -1,82 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "image.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_buttonimage.h" - -widget_t *button_image_new(image_t *image, int x, int y, void (*fce_event) (void *)) -{ - widget_buttonimage_t *new; - - new = malloc(sizeof(widget_buttonimage_t)); - new->image = image; - new->w = image->w / 2; - new->h = image->h; - new->active = 0; - new->fce_event = fce_event; - - return widget_new(WIDGET_TYPE_BUTTONIMAGE, x, y, new->w, new->h, new); -} - -void button_image_set_active(widget_t *widget, bool_t active) -{ - widget_buttonimage_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTONIMAGE); - - p = (widget_buttonimage_t *) widget->private_data; - p->active = active; -} - -void button_image_draw(widget_t *widget) -{ - widget_buttonimage_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTONIMAGE); - - p = (widget_buttonimage_t *) widget->private_data; - image_draw(p->image, widget->x, widget->y, p->active *p->w, 0, p->w, p->h); -} - -void button_image_event(widget_t *widget) -{ - widget_buttonimage_t *p; - static int my_time = 0; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTONIMAGE); - - p = (widget_buttonimage_t *) widget->private_data; - - if (my_time > 0) { - my_time--; - return; - } - - if (mouse_buffer_is_on_area(widget->x, widget->y, p->w, p->h, MOUSE_BUF_CLICK)) { - my_time = WIDGET_BUTTONIMAGE_TIME; - p->active = 1; - p->fce_event(widget); - } -} - -void button_image_destroy(widget_t *widget) -{ - widget_buttonimage_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_BUTTONIMAGE); - - p = (widget_buttonimage_t *) widget->private_data; - - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_buttonimage.h b/src/widget/widget_buttonimage.h deleted file mode 100644 index fff2658..0000000 --- a/src/widget/widget_buttonimage.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WIDGET_BUTTONIMAGE_H -#define WIDGET_BUTTONIMAGE_H - -#include "main.h" -#include "image.h" -#include "widget.h" - -#define WIDGET_BUTTONIMAGE_TIME 10 - -typedef struct widget_buttonimageimage { - int w, h; - int active; - image_t *image; - void (*fce_event) (void *); -} widget_buttonimage_t; - -extern widget_t *button_image_new(image_t *image, int x, int y, void (*fce_event) (void *)); - -extern void button_image_set_active(widget_t *widget, bool_t active); -extern void button_image_draw(widget_t *widget); -extern void button_image_event(widget_t *widget); -extern void button_image_destroy(widget_t *widget); - -#endif /* WIDGET_BUTTONIMAGE_H */ diff --git a/src/widget/widget_catchkey.c b/src/widget/widget_catchkey.c deleted file mode 100644 index 814cfa1..0000000 --- a/src/widget/widget_catchkey.c +++ /dev/null @@ -1,141 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_catchkey.h" - -widget_t *catch_key_new(int key, int x, int y, void *event) -{ - widget_catchkey_t *new; - - new = malloc(sizeof(widget_catchkey_t)); - new->key = key; - new->fce_event = event; - new->active = FALSE; - - return widget_new(WIDGET_TYPE_CATCHKEY, x, y, WIDGET_CATCHKEY_WIDTH, - WIDGET_CATCHKEY_HEIGHT, new); -} - -int catch_key_get(widget_t *widget) -{ - widget_catchkey_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CATCHKEY); - - p = (widget_catchkey_t *) widget->private_data; - - return p->key; -} - -void catch_key_set(widget_t *widget, int key) -{ - widget_catchkey_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CATCHKEY); - - p = (widget_catchkey_t *) widget->private_data; - - p->key = key; -} - -void catch_key_draw(widget_t *widget) -{ - widget_catchkey_t *p; - char *name; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CATCHKEY); - - p = (widget_catchkey_t *) widget->private_data; - name = NULL; - - if (p->key != WIDGET_CATCHKEY_NOKEY) { - name = (char *) SDL_GetKeyName(p->key); - } - - if (name == NULL) { - name = "no key"; - } - - if (p->active) { - font_draw(name, widget->x, widget->y, COLOR_RED); - } else { - font_draw(name, widget->x, widget->y, COLOR_WHITE); - } -} - -static int getPressAnyKey() -{ - Uint8 *mapa; - int i; - - mapa = SDL_GetKeyState(NULL); - - for (i = SDLK_FIRST; i <= SDLK_COMPOSE; i++) { - if (i == SDLK_NUMLOCK || /* black key */ - i == SDLK_CAPSLOCK || - i == SDLK_SCROLLOCK) { - continue; - } - - if (mapa[i] == SDL_PRESSED) { - return i; - } - } - - return WIDGET_CATCHKEY_NOKEY; -} - -void catch_key_event(widget_t *widget) -{ - widget_catchkey_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CATCHKEY); - - p = (widget_catchkey_t *) widget->private_data; - - if (mouse_buffer_is_on_area(0, 0, 0, 0, MOUSE_BUF_AREA_NONE|MOUSE_BUF_CLICK)) { - if (mouse_buffer_is_on_area(widget->x, widget->y, - WIDGET_CATCHKEY_WIDTH, - WIDGET_CATCHKEY_HEIGHT, - MOUSE_BUF_CLICK)) { - p->active = TRUE; - } else { - p->active = FALSE; - } - } - - if (p->active == TRUE) { - int key; - - key = getPressAnyKey(); - - if (key != WIDGET_CATCHKEY_NOKEY) { - p->key = key; - p->fce_event(widget); - p->active = FALSE; - } - } -} - -void catch_key_destroy(widget_t *widget) -{ - widget_catchkey_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CATCHKEY); - - p = (widget_catchkey_t *) widget->private_data; - - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_catchkey.h b/src/widget/widget_catchkey.h deleted file mode 100644 index d922241..0000000 --- a/src/widget/widget_catchkey.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef WIDGET_CATCHKEY_H -#define WIDGET_CATCHKEY_H - -#include "main.h" -#include "widget.h" - -#define WIDGET_CATCHKEY_NOKEY -1 - -#define WIDGET_CATCHKEY_WIDTH 110 -#define WIDGET_CATCHKEY_HEIGHT 16 - -typedef struct widget_catchkey_struct { - int w, h; - int key; - bool_t active; - void (*fce_event) (void *p); -} widget_catchkey_t; - -extern widget_t *catch_key_new(int key, int x, int y, void *event); -extern int catch_key_get(widget_t *p); -extern void catch_key_set(widget_t *p, int key); -extern void catch_key_draw(widget_t *p); -extern void catch_key_event(widget_t *p); -extern void catch_key_destroy(widget_t *p); - -#endif /* WIDGET_CATCHKEY_H */ diff --git a/src/widget/widget_check.c b/src/widget/widget_check.c deleted file mode 100644 index f39c6ad..0000000 --- a/src/widget/widget_check.c +++ /dev/null @@ -1,114 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "image.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_check.h" - -widget_t *check_new(int x, int y, bool_t status, void (*fce_event) (void *)) -{ - widget_check_t *new; - - new = malloc(sizeof(widget_check_t)); - new->time = 0; - new->status = status; - new->fce_event = fce_event; - - return widget_new(WIDGET_TYPE_CHECK, x, y, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT, new); -} - -void check_draw(widget_t *widget) -{ - widget_check_t *p; - static image_t *g_check = NULL; - int x, y; - int offset; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHECK); - - p = (widget_check_t *) widget->private_data; - - interface_get_mouse_position(&x, &y); - - if (g_check == NULL) { - g_check = image_add("check.png", IMAGE_ALPHA, "check", IMAGE_GROUP_BASE); - } - - if (p->status == TRUE) { - offset = 0; - } else { - offset = WIDGET_CHECK_WIDTH; - } - - image_draw(g_check, widget->x, widget->y, offset, 0, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT); -} - -void check_event(widget_t *widget) -{ - widget_check_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHECK); - - p = (widget_check_t *) widget->private_data; - - if (p->time > 0) { - p->time--; - return; - } - - if (mouse_buffer_is_on_area(widget->x, widget->y, WIDGET_CHECK_WIDTH, - WIDGET_CHECK_HEIGHT, MOUSE_BUF_CLICK)) { - if (p->status == TRUE) { - p->status = FALSE; - p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; - } else { - p->status = TRUE; - p->time = WIDGET_CHECK_TIME_SWITCH_STATUS; - } - - if (p->fce_event != NULL) { - p->fce_event(widget); - } - } -} - -bool_t check_get_status(widget_t *widget) -{ - widget_check_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHECK); - - p = (widget_check_t *) widget->private_data; - - return p->status; -} - -void check_set_status(widget_t *widget, bool_t status) -{ - widget_check_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHECK); - - p = (widget_check_t *) widget->private_data; - p->status = status; -} - -void check_destroy(widget_t *widget) -{ - widget_check_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHECK); - - p = (widget_check_t *) widget->private_data; - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_check.h b/src/widget/widget_check.h deleted file mode 100644 index 3df8c23..0000000 --- a/src/widget/widget_check.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef WIDGET_CHECK_H -#define WIDGET_CHECK_H - -#include "main.h" -#include "widget.h" - -#define WIDGET_CHECK_TIME_SWITCH_STATUS 5 - -#define WIDGET_CHECK_WIDTH 25 -#define WIDGET_CHECK_HEIGHT 25 - -typedef struct widget_check { - bool_t status; - int time; - void (*fce_event) (void *); -} widget_check_t; - -extern widget_t *check_new(int x, int y, bool_t status, void (*fce_event) (void *)); -extern void check_draw(widget_t *widget); -extern void check_event(widget_t *widget); -extern bool_t check_get_status(widget_t *widget); -extern void check_set_status(widget_t *widget, bool_t status); -extern void check_destroy(widget_t *widget); - -#endif /* WIDGET_CHECK_H */ diff --git a/src/widget/widget_choicegroup.c b/src/widget/widget_choicegroup.c deleted file mode 100644 index 2c3338f..0000000 --- a/src/widget/widget_choicegroup.c +++ /dev/null @@ -1,143 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "image.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_choicegroup.h" - -widget_t *choice_group_new(int x, int y, bool_t status, list_t *list, void (*fce_event) (void *)) -{ - widget_choicegroup_t *new; - widget_t *widget; - - new = malloc(sizeof(widget_choicegroup_t)); - new->time = 0; - new->status = status; - new->fce_event = fce_event; - new->list = list; - - widget = widget_new(WIDGET_TYPE_CHOICE, x, y, WIDGET_CHOICEGROUP_WIDTH, - WIDGET_CHOICEGROUP_HEIGHT, new); - - list_add(new->list, widget); - - return widget; -} - -void choice_group_draw(widget_t *widget) -{ - widget_choicegroup_t *p; - static image_t *g_choicegroup = NULL; - int offset; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - - if (g_choicegroup == NULL) { - g_choicegroup = image_add("choice.png", IMAGE_ALPHA, "choicegroup", IMAGE_GROUP_BASE); - } - - if (p->status == TRUE) { - offset = 0; - } else { - offset = WIDGET_CHOICEGROUP_WIDTH; - } - - image_draw(g_choicegroup, widget->x, widget->y, offset, 0, - WIDGET_CHOICEGROUP_WIDTH, WIDGET_CHOICEGROUP_HEIGHT); -} - -void choice_group_set_active(widget_t *widget) -{ - widget_choicegroup_t *p; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - - for (i = 0; i < p->list->count; i++) { - widget_t *this; - widget_choicegroup_t *thisChoice; - - this = (widget_t *) p->list->list[i]; - thisChoice = (widget_choicegroup_t *) this->private_data; - - if (widget == this) { - thisChoice->status = TRUE; - thisChoice->time = WIDGET_CHOICEGROUP_TIME_SWITCH_STATUS; - thisChoice->fce_event(p); - } else { - thisChoice->status = FALSE; - } - } -} - -bool_t choice_group_get_status(widget_t *widget) -{ - widget_choicegroup_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - - return p->status; -} - -void choiceGroup_set_status(widget_t *widget, bool_t status) -{ - widget_choicegroup_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - p->status = status; -} - -void choice_group_event(widget_t *widget) -{ - widget_choicegroup_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - - if (p->time > 0) { - p->time--; - return; - } - - if (mouse_buffer_is_on_area(widget->x, widget->y, - WIDGET_CHOICEGROUP_WIDTH, - WIDGET_CHOICEGROUP_HEIGHT, - MOUSE_BUF_CLICK)) { - choice_group_set_active(widget); - } -} - -void choice_group_destroy(widget_t *widget) -{ - widget_choicegroup_t *p; - int my_index; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CHOICE); - - p = (widget_choicegroup_t *) widget->private_data; - my_index = list_search(p->list, widget); - assert(my_index != -1); - list_del(p->list, my_index); - - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_choicegroup.h b/src/widget/widget_choicegroup.h deleted file mode 100644 index 9bdadfc..0000000 --- a/src/widget/widget_choicegroup.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef WIDGET_CHOICEGROUP_H -#define WIDGET_CHOICEGROUP_H - -#include "main.h" -#include "list.h" -#include "widget.h" - -#define WIDGET_CHOICEGROUP_TIME_SWITCH_STATUS 5 - -#define WIDGET_CHOICEGROUP_WIDTH 25 -#define WIDGET_CHOICEGROUP_HEIGHT 25 - -typedef struct widget_choicegroup { - bool_t status; - int time; - void (*fce_event) (void *); - list_t *list; -} widget_choicegroup_t; - -extern widget_t *choice_group_new(int x, int y, bool_t status, list_t *list, void (*fce_event) (void *)); - -extern void choice_group_draw(widget_t *p); -extern void choice_group_set_active(widget_t *widget); -extern bool_t choice_group_get_status(widget_t *widget); -extern void choiceGroup_set_status(widget_t *widget, bool_t status); -extern void choice_group_event(widget_t *p); -extern void choice_group_destroy(widget_t *p); - -#endif /* WIDGET_CHOICEGROUP_H */ diff --git a/src/widget/widget_container.c b/src/widget/widget_container.c deleted file mode 100644 index 5de850c..0000000 --- a/src/widget/widget_container.c +++ /dev/null @@ -1,108 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "widget.h" -#include "widget_container.h" - -widget_t *widget_container_new(int x, int y, int w, int h) -{ - widget_container_t *new; - - new = malloc(sizeof(widget_container_t)); - new->listWidget = list_new(); - - return widget_new(WIDGET_TYPE_CONTAINER, x, y, w, h, new); -} - -void widget_container_add(widget_t *widget, widget_t *widget_add) -{ - widget_container_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CONTAINER); - - p = (widget_container_t *) widget->private_data; - list_add(p->listWidget, widget_add); -} - -void widget_set_layout_table(widget_t *widget, int countW, int countH, int w, int h) -{ - widget_container_t *p; - int i, j; - int count; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CONTAINER); - - p = (widget_container_t *) widget->private_data; - - count = 0; - - for (i = 0; i < countH; i++) { - for (j = 0; j < countW; j++) { - widget_t *widget_tmp; - - if (count >= p->listWidget->count) { - break; - } - - widget_tmp = (widget_t *) p->listWidget->list[count]; - - widget_set_location(widget_tmp, widget->x + j * w, widget->y + i * h); - count++; - } - } -} - -void widget_container_draw(widget_t *widget) -{ - widget_container_t *p; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CONTAINER); - - p = (widget_container_t *) widget->private_data; - - for (i = 0; i < p->listWidget->count; i++) { - widget_t *widget; - - widget = (widget_t *) p->listWidget->list[i]; - - widget_draw(widget); - } -} - -void widget_container_event(widget_t *widget) -{ - widget_container_t *p; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CONTAINER); - - p = (widget_container_t *) widget->private_data; - - for (i = 0; i < p->listWidget->count; i++) { - widget_t *widget; - - widget = (widget_t *) p->listWidget->list[i]; - - widget_event(widget); - } -} - -void widget_container_destroy(widget_t *widget) -{ - widget_container_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_CONTAINER); - - p = (widget_container_t *) widget->private_data; - list_destroy(p->listWidget); - free(p); - - widget_destroy(widget); -} diff --git a/src/widget/widget_container.h b/src/widget/widget_container.h deleted file mode 100644 index 8d9320c..0000000 --- a/src/widget/widget_container.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef WIDGET_CONTAINER_H -#define WIDGET_CONTAINER_H - -#include "list.h" -#include "widget.h" - -typedef struct widget_container { - list_t *listWidget; -} widget_container_t; - -extern widget_t *widget_container_new(int x, int y, int w, int h); -extern void widget_container_add(widget_t *widget, widget_t *widget_add); -extern void widget_set_layout_table(widget_t *widget, int countW, int countH, int w, int h); -extern void widget_container_draw(widget_t *widget); -extern void widget_container_event(widget_t *widget); -extern void widget_container_destroy(widget_t *widget); - -#endif /* WIDGET_CONTAINER_H */ diff --git a/src/widget/widget_image.c b/src/widget/widget_image.c deleted file mode 100644 index 51f935a..0000000 --- a/src/widget/widget_image.c +++ /dev/null @@ -1,51 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include <stdlib.h> -#include "main.h" -#include "image.h" -#include "interface.h" -#include "font.h" - -#include "widget.h" -#include "widget_image.h" - -widget_t *wid_image_new(int x, int y, image_t *image) -{ - widget_image_t *new; - - new = malloc(sizeof(widget_image_t)); - new->image = image; - - return widget_new(WIDGET_TYPE_IMAGE, x, y, image->w, image->h, new); -} - -void wid_image_draw(widget_t *widget) -{ - widget_image_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_IMAGE); - - p = (widget_image_t *) widget->private_data; - image_draw(p->image, widget->x, widget->y, 0, 0, p->image->w, p->image->h); -} - -void wid_image_event(widget_t *widget) -{ - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_IMAGE); -} - -void wid_image_destroy(widget_t *widget) -{ - widget_image_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_IMAGE); - - p = (widget_image_t *) widget->private_data; - - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_image.h b/src/widget/widget_image.h deleted file mode 100644 index ad7104d..0000000 --- a/src/widget/widget_image.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef WIDGET_IMAGE_H -#define WIDGET_IMAGE_H - -#include "main.h" -#include "interface.h" -#include "widget.h" - -typedef struct widget_image { - image_t *image; -} widget_image_t; - -extern widget_t *wid_image_new(int x, int y, image_t *image); -extern void wid_image_draw(widget_t *p); -extern void wid_image_event(widget_t *p); -extern void wid_image_destroy(widget_t *p); - -#endif /* WIDGET_IMAGE_H */ diff --git a/src/widget/widget_label.c b/src/widget/widget_label.c deleted file mode 100644 index b696302..0000000 --- a/src/widget/widget_label.c +++ /dev/null @@ -1,66 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "widget_label.h" -#include "widget.h" - -widget_t *label_new(char *text, int x, int y, int bind) -{ - widget_label_t *new; - - new = malloc(sizeof(widget_label_t)); - new->text = strdup(text); - new->bind = bind; - font_text_size(text, &(new->w), &(new->h)); - - return widget_new(WIDGET_TYPE_LABEL, x, y, new->w, new->h, new); -} - -void label_draw(widget_t *widget) -{ - widget_label_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_LABEL); - - p = (widget_label_t *) widget->private_data; - - switch (p->bind) { - case WIDGET_LABEL_RIGHT: - font_draw(p->text, widget->x - p->w, widget->y + p->h / 2, COLOR_WHITE); - break; - case WIDGET_LABEL_LEFT: - font_draw(p->text, widget->x, widget->y + widget->h / 2, COLOR_WHITE); - break; - case WIDGET_LABEL_CENTER: - font_draw(p->text, widget->x - p->w / 2, widget->y + p->h / 2, COLOR_WHITE); - break; - default: - fatal("Unknown dimension for label placement"); - break; - } -} - -void label_event(widget_t *widget) -{ - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_LABEL); -} - -void label_destroy(widget_t *widget) -{ - widget_label_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_LABEL); - - p = (widget_label_t *) widget->private_data; - - free(p->text); - free(p); - - widget_destroy(widget); -} diff --git a/src/widget/widget_label.h b/src/widget/widget_label.h deleted file mode 100644 index bd1b531..0000000 --- a/src/widget/widget_label.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef WIDGET_LABEL_H -#define WIDGET_LABEL_H - -#include "main.h" -#include "widget.h" - -#define WIDGET_LABEL_RIGHT 1 -#define WIDGET_LABEL_LEFT 2 -#define WIDGET_LABEL_CENTER 3 - -typedef struct widget_label { - int w, h; - char *text; - int bind; -} widget_label_t; - -extern widget_t *label_new(char *text, int x, int y, int bind); -extern void label_draw(widget_t *widget); -extern void label_event(widget_t *widget); -extern void label_destroy(widget_t *widget); - -#endif /* WIDGET_LABEL_H */ diff --git a/src/widget/widget_select.c b/src/widget/widget_select.c deleted file mode 100644 index 13ce483..0000000 --- a/src/widget/widget_select.c +++ /dev/null @@ -1,168 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_select.h" - -widget_t *select_new(int x, int y, void (*fce_event) (void *)) -{ - widget_select_t *new; - - new = malloc(sizeof(widget_select_t)); - new->select = -1; - new->fce_event = fce_event; - new->list = list_new(); - - return widget_new(WIDGET_TYPE_SELECT, x, y, 0, 0, new); -} - -char *select_get_item(widget_t *widget) -{ - widget_select_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - - if (p->select == -1) { - return NULL; - } - - return (char *) p->list->list[p->select]; -} - -int select_get_index(widget_t *widget) -{ - widget_select_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - - return p->select; -} - -void select_add(widget_t *widget, char *s) -{ - widget_select_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - - list_add(p->list, strdup(s)); -} - -void select_remove_all(widget_t *widget) -{ - widget_select_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - - while (p->list->count > 0) { - list_del_item(p->list, 0, free); - } -} - -void select_draw(widget_t *widget) -{ - widget_select_t *p; - int x, y, w, h; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - interface_get_mouse_position(&x, &y); - p = (widget_select_t *) widget->private_data; - - for (i = 0; i < p->list->count; i++) { - char *line; - - line = (char *) p->list->list[i]; - - font_text_size(line, &w, &h); -/* - if (x > widget->x && y > widget->y + i * 20 && - x < widget->x + w && y < widget->y + i * 20 + h) { - font_draw(line, widget->x, widget->y + i * 20, COLOR_YELLOW); - } else { - if (p->select == i) { - font_draw(line, widget->x, widget->y + i * 20, COLOR_RED); - } else { - font_draw(line, widget->x, widget->y + i * 20, COLOR_WHITE); - } - } -*/ - if (mouse_buffer_is_on_area(widget->x, widget->y + i * 20, - w, h, MOUSE_BUF_MOTION)) { - font_draw(line, widget->x, widget->y + i * 20, COLOR_YELLOW); - } else { - if (p->select == i) { - font_draw(line, widget->x, widget->y + i * 20, COLOR_RED); - } else { - font_draw(line, widget->x, widget->y + i * 20, COLOR_WHITE); - } - } - - } -} - -void select_event(widget_t *widget) -{ - widget_select_t *p; - int x, y, w, h; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - interface_get_mouse_position(&x, &y); - - for (i = 0; i < p->list->count; i++) { - char *line; - - line = (char *) p->list->list[i]; - - font_text_size(line, &w, &h); - - if (mouse_buffer_is_on_area(widget->x, widget->y+i * 20, w, h, MOUSE_BUF_CLICK)) { - p->select = i; - p->fce_event(p); - } -/* - if (x > widget->x && y > widget->y + i * 20 && - x < widget->x + w && y < widget->y + i * 20 + h && - interface_is_mouse_clicket()) { - p->select = i; - p->fce_event(p); - } -*/ - } -} - -void select_destroy(widget_t *widget) -{ - widget_select_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_SELECT); - - p = (widget_select_t *) widget->private_data; - - list_destroy_item(p->list, free); - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_select.h b/src/widget/widget_select.h deleted file mode 100644 index 11116a9..0000000 --- a/src/widget/widget_select.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WIDGET_SELECT_H -#define WIDGET_SELECT_H - -#include "main.h" -#include "list.h" -#include "widget.h" - -typedef struct widget_select { - int w, h; - list_t *list; - int select; - void (*fce_event) (void *); -} widget_select_t; - -extern widget_t *select_new(int x, int y, void (*fce_event) (void *)); -extern char *select_get_item(widget_t *widget); -extern int select_get_index(widget_t *widget); -extern void select_add(widget_t *widget, char *s); -extern void select_remove_all(widget_t *widget); -extern void select_draw(widget_t *widget); -extern void select_event(widget_t *widget); -extern void select_destroy(widget_t *widget); - -#endif /* WIDGET_SELECT_H */ diff --git a/src/widget/widget_statusbar.c b/src/widget/widget_statusbar.c deleted file mode 100644 index c2b48a4..0000000 --- a/src/widget/widget_statusbar.c +++ /dev/null @@ -1,146 +0,0 @@ -#include <stdlib.h> -#include <assert.h> - -#include "main.h" -#include "interface.h" -#include "image.h" -#include "font.h" -#include "widget_statusbar.h" -#include "widget.h" -#include "mouse_buffer.h" - -typedef struct item_struct { - widget_t *widget; - list_t *text; -} item_t; - -static item_t *item_new(widget_t *widget, char *text) -{ - item_t *item; - char *copy; - char *s; - - assert(widget != NULL); - assert(text != NULL); - - item = malloc(sizeof(item_t)); - item->widget = widget; - item->text = list_new(); - - copy = strdup(text); - - s = strtok(copy, "\n"); - - while(s != NULL) { - list_add(item->text, strdup(s)); - s = strtok(NULL, "\n"); - } - - free(copy); - - return item; -} - -static void item_destroy(item_t *item) -{ - assert(item != NULL); - - list_destroy_item(item->text, free); - free(item); -} - -widget_t *statusbar_new(int x, int y) -{ - widget_statusbar_t *new; - - new = malloc(sizeof(widget_statusbar_t)); - new->selectElement = WIDGET_STATUSBAR_NONE_SELECT_ELEMENT; - new->listElement = list_new(); - - return widget_new(WIDGET_TYPE_STATUSBAR, x, y, WIDGET_STATUSBAR_WIDTH, WIDGET_STATUSBAR_HEIGHT, new); -} - -void statusbar_add(widget_t *widget, widget_t *widget_catch, char *text) -{ - widget_statusbar_t *p; - - assert(widget != NULL); - assert(widget_catch != NULL); - assert(text != NULL); - assert(widget->type == WIDGET_TYPE_STATUSBAR); - - p = (widget_statusbar_t *) widget->private_data; - - list_add(p->listElement, item_new(widget_catch, text)); -} - -void statusbar_draw(widget_t *widget) -{ - static image_t *g_image = NULL; - widget_statusbar_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_STATUSBAR); - - p = (widget_statusbar_t *) widget->private_data; - - if (g_image == NULL) { - g_image = image_add("frame.png", IMAGE_ALPHA, "frame", IMAGE_GROUP_BASE); - } - - image_draw(g_image, widget->x, widget->y, 0, 0, g_image->w, g_image->h); - - if (p->selectElement != WIDGET_STATUSBAR_NONE_SELECT_ELEMENT) { - item_t *item; - int i; - - item = (item_t *) p->listElement->list[p->selectElement]; - - for (i = 0 ; i < item->text->count; i++) { - char *s; - - s = (char *) item->text->list[i]; - font_draw(s, widget->x, widget->y + i * (font_get_size() + 5), COLOR_WHITE); - } - } -} - -void statusbar_event(widget_t *widget) -{ - widget_statusbar_t *p; - int i; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_STATUSBAR); - - p = (widget_statusbar_t *) widget->private_data; - - p->selectElement = WIDGET_STATUSBAR_NONE_SELECT_ELEMENT; - - for (i = 0 ; i < p->listElement->count; i++) { - item_t *item; - - item = (item_t *) p->listElement->list[i]; - - if (mouse_buffer_is_on_area(item->widget->x, item->widget->y, - item->widget->w, item->widget->h, - MOUSE_BUF_MOTION)) { - p->selectElement = i; - } - } -} - -void statusbar_destroy(widget_t *widget) -{ - widget_statusbar_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_STATUSBAR); - - p = (widget_statusbar_t *) widget->private_data; - - list_destroy_item(p->listElement, item_destroy); - free(p); - - widget_destroy(widget); -} diff --git a/src/widget/widget_statusbar.h b/src/widget/widget_statusbar.h deleted file mode 100644 index 217c1a5..0000000 --- a/src/widget/widget_statusbar.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WIDGET_STATUSBAR_H -#define WIDGET_STATUSBAR_H - -#include "main.h" -#include "widget.h" -#include "list.h" - -typedef struct widget_statusbar { - list_t *listElement; - int selectElement; -} widget_statusbar_t; - -#define WIDGET_STATUSBAR_NONE_SELECT_ELEMENT -1 - -#define WIDGET_STATUSBAR_WIDTH 250 -#define WIDGET_STATUSBAR_HEIGHT 65 - -extern widget_t *statusbar_new(int x, int y); -extern void statusbar_add(widget_t *widget, widget_t *widget_catch, char *text); -extern void statusbar_draw(widget_t *widget); -extern void statusbar_event(widget_t *widget); -extern void statusbar_destroy(widget_t *widget); - -#endif /* WIDGET_STATUSBAR_H */ diff --git a/src/widget/widget_textfield.c b/src/widget/widget_textfield.c deleted file mode 100644 index 53d03a3..0000000 --- a/src/widget/widget_textfield.c +++ /dev/null @@ -1,284 +0,0 @@ -#include <stdlib.h> -#include <assert.h> -#include <SDL_keyboard.h> - -#include "main.h" -#include "interface.h" -#include "font.h" -#include "image.h" -#include "mouse_buffer.h" - -#include "widget.h" -#include "widget_textfield.h" - -#include "keyboardBuffer.h" - -widget_t *text_field_new(char *text, int filter, int x, int y) -{ - widget_textfield_t *new; - - new = malloc(sizeof(widget_textfield_t)); - memset(new, 0, sizeof(widget_textfield_t)); - - strcpy(new->text, text); - new->time = 0; - new->timeBlick = 0; - new->atime = 0; - new->active = FALSE; - new->filter = filter; - font_text_size(text, &new->w, &new->h); - - return widget_new(WIDGET_TYPE_TEXTFILED, x, y, WIDGET_TEXTFIELD_WIDTH, WIDGET_TEXTFIELD_HEIGHT, new); -} - -static void drawBackground(widget_t *widget) -{ - static image_t *g_textfield0 = NULL; - static image_t *g_textfield1 = NULL; - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - - if (g_textfield0 == NULL) { - g_textfield0 = image_add("textfield0.png", IMAGE_ALPHA, "textfiled0", IMAGE_GROUP_BASE); - } - - if (g_textfield1 == NULL) { - g_textfield1 = image_add("textfield1.png", IMAGE_ALPHA, "textfiled1", IMAGE_GROUP_BASE); - } - - if (p->active) { - image_draw(g_textfield1, widget->x, widget->y, 0, 0, g_textfield1->w, g_textfield1->h); - } else { - image_draw(g_textfield0, widget->x, widget->y, 0, 0, g_textfield0->w, g_textfield0->h); - } -} - -static void drawText(widget_t *widget) -{ - char str[STR_SIZE]; - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - - strcpy(str, p->text); - - if (p->timeBlick > WIDGET_TEXTFIELD_TIME_BLICK_CURSOR / 2 && p->active == TRUE) { - strcat(str, "\f"); - } - - p->timeBlick++; - - if (p->timeBlick == WIDGET_TEXTFIELD_TIME_BLICK_CURSOR) { - p->timeBlick = 0; - } - - /* - font_draw(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+p->h/2, COLOR_WHITE); - printf("p->y: %d\nheight: %d\n p->h: %d\n", p->y, WIDGET_TEXTFIELD_HEIGHT, p->h); - */ - - font_draw(str, widget->x + WIDGET_TEXTFIELD_TEXT_OFFSET_X, - widget->y + WIDGET_TEXTFIELD_HEIGHT / 2 - p->h / 2, - COLOR_WHITE); -} - -void text_field_draw(widget_t *widget) -{ - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - drawBackground(widget); - drawText(widget); -} - -void text_field_set_text(widget_t *widget, char *text) -{ - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - - memset(p->text, 0, STR_SIZE); - strcpy(p->text, text); - font_text_size(text, &p->w, &p->h); -} - -char *text_field_get_text(widget_t *widget) -{ - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - return p->text; -} - -static void checkText(widget_textfield_t *p, unsigned len) -{ - unsigned int i; - - for (i = 0; i < len; i++) { - char c; - bool_t isDel; - - c = p->text[i]; - isDel = TRUE; - - switch (p->filter) { - case WIDGET_TEXTFIELD_FILTER_ALL: - isDel = FALSE; - break; - - case WIDGET_TEXTFIELD_FILTER_NUM: - if (c >= '0' && c <= '9') { - isDel = FALSE; - } - break; - - case WIDGET_TEXTFIELD_FILTER_ALPHANUM: - if ((c >= '0' && c <= '9') || - (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c == '_' || c == '-')) { - isDel = FALSE; - } - break; - - case WIDGET_TEXTFIELD_FILTER_IP_OR_DOMAIN: - if ((c >= '0' && c <= '9') || - (c == '.' || c == ':' || c == '-') || - (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z')) { - isDel = FALSE; - } - break; - - default: - fatal("Bad text filter!"); - break; - } - - if (isDel) { - memmove(p->text + i, p->text + i + 1, len - i); - len = strlen(p->text); - i--; - } - } - - font_text_size(p->text, &p->w, &p->h); -} - - -static void readKey(widget_textfield_t *p) -{ - int len; - int i; - - if (p->active == FALSE) { - return; - } - - len = strlen(p->text); - - for (i = 0; keyboard_buffer_is_any_key(); i++) { - SDL_keysym k = keyboard_buffer_pop(); - - char *name = (char *) SDL_GetKeyName(k.sym); - - if (!name) { - continue; - } - - if (len >= STR_SIZE) { - break; - } - - if (k.sym == SDLK_RETURN) { - break; - } - - char c = '\0'; - - switch (strlen(name)) { - case 1: - c = name[0]; - break; - - case 3: - c = name[1]; - break; - } - - if ((k.unicode & 0xFF80) == 0) { - c = k.unicode & 0x7F; - } - - if (k.sym == SDLK_SPACE || k.sym == SDLK_TAB) { - c = ' '; - } else if (k.sym == SDLK_BACKSPACE) { - if (len > 0) { - p->text[len-1] = '\0'; - checkText(p, len); - } - - break; - } - - if (c == '\0' || p->w > WIDGET_TEXTFIELD_WIDTH - 40) { - continue; - } - - p->text[len] = c; - - len++; - checkText(p, len); - } -} - -void text_field_event(widget_t *widget) -{ - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - - if (mouse_buffer_is_on_area(0, 0, 0, 0, MOUSE_BUF_AREA_NONE|MOUSE_BUF_CLICK)) { - if (mouse_buffer_is_on_area(widget->x, widget->y, - WIDGET_TEXTFIELD_WIDTH, - WIDGET_TEXTFIELD_HEIGHT, - MOUSE_BUF_MOTION)) { - p->active = TRUE; - interface_enable_keyboard_buffer(); - keyboard_buffer_clear(); - } else { - p->active = FALSE; - } - } - - readKey(p); -} - -void text_field_destroy(widget_t *widget) -{ - widget_textfield_t *p; - - assert(widget != NULL); - assert(widget->type == WIDGET_TYPE_TEXTFILED); - - p = (widget_textfield_t *) widget->private_data; - - free(p); - widget_destroy(widget); -} diff --git a/src/widget/widget_textfield.h b/src/widget/widget_textfield.h deleted file mode 100644 index ef55b5f..0000000 --- a/src/widget/widget_textfield.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef WIDGET_TEXTFIELD_H -#define WIDGET_TEXTFIELD_H - -#include "main.h" -#include "widget.h" - -#define WIDGET_TEXTFIELD_WIDTH 175 -#define WIDGET_TEXTFIELD_HEIGHT 36 - -#define WIDGET_TEXTFIELD_TEXT_OFFSET_X 10 - -#define WIDGET_TEXTFIELD_TIME_MULTIPLE 10 -#define WIDGET_TEXTFIELD_TIME_READ_KEY 2 -#define WIDGET_TEXTFIELD_TIME_BLICK_CURSOR 20 - -#define WIDGET_TEXTFIELD_FILTER_ALL 0 -#define WIDGET_TEXTFIELD_FILTER_NUM 2 -#define WIDGET_TEXTFIELD_FILTER_ALPHANUM 3 -#define WIDGET_TEXTFIELD_FILTER_IP_OR_DOMAIN 4 - -typedef struct widget_textfield { - int w, h; - int canWrite; - int timeBlick; - int time; - int atime; - int filter; - char text[STR_SIZE]; - bool_t active; -} widget_textfield_t; - -extern widget_t *text_field_new(char *text, int filter, int x, int y); -extern void text_field_set_text(widget_t *widget, char *text); -extern char *text_field_get_text(widget_t *widget); -extern void text_field_draw(widget_t *widget); -extern void text_field_event(widget_t *widget); -extern void text_field_destroy(widget_t *widget); - -#endif /* WIDGET_TEXTFIELD_H */ diff --git a/src/window.c b/src/window.c new file mode 100644 index 0000000..fdcd31f --- /dev/null +++ b/src/window.c @@ -0,0 +1,51 @@ +/* + * Tuxánci 2 - A first person shooter + * Copyright (C) 2007-2011 Tuxánci Development Team + * Copyright (C) 2025-2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdbool.h> +#include "ta_sdl.h" +#include "version.h" +#include "logging.h" + +#define TA_DEFAULT_WINDOW_TITLE "Tuxánci " TA_VERSION +#define TA_DEFAULT_WINDOW_WIDTH 1920 +#define TA_DEFAULT_WINDOW_HEIGHT 1080 + +int ta_window_width = TA_DEFAULT_WINDOW_WIDTH; +int ta_window_height = TA_DEFAULT_WINDOW_HEIGHT; +bool ta_window_fullscreen = true; + +SDL_Window *ta_window; + +void ta_window_init(void) { + ta_sdl_init(SDL_INIT_VIDEO); + + ta_window = ta_sdl_create_window(TA_DEFAULT_WINDOW_TITLE, TA_DEFAULT_WINDOW_WIDTH, TA_DEFAULT_WINDOW_HEIGHT); +} + +void ta_window_update(void) { + ta_sdl_get_window_size(ta_window, &ta_window_width, &ta_window_height); + ta_sdl_set_window_fullscreen(ta_window, ta_window_fullscreen); +} + +void ta_window_shutdown(void) { + if (ta_window) { + ta_log("Destroying SDL window"); + SDL_DestroyWindow(ta_window); + } +}
\ No newline at end of file |