From c0cc0a9346c44c655aead1376cb2f123b6edb06c Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 16 Feb 2013 20:01:00 +0000 Subject: [PATCH] [Add] Start of toolkit framework, we will manage windows etc here. [Add] Pause/Resume --- bin/conf | 40 ++++++++++++++++++++++ dat/outfit.xml | 2 +- src/conf.c | 6 ++++ src/conf.h | 4 +++ src/input.c | 32 ++++++++++++++---- src/main.c | 23 +++++++------ src/pause.c | 61 +++++++++++++++++++++++++++++++++ src/pause.h | 7 ++++ src/space.c | 19 ++++++----- src/toolkit.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/toolkit.h | 18 ++++++++++ src/weapon.c | 20 +++++++++++ src/weapon.h | 4 +++ 13 files changed, 300 insertions(+), 27 deletions(-) create mode 100644 bin/conf create mode 100644 src/pause.c create mode 100644 src/pause.h create mode 100644 src/toolkit.c create mode 100644 src/toolkit.h diff --git a/bin/conf b/bin/conf new file mode 100644 index 0000000..f854873 --- /dev/null +++ b/bin/conf @@ -0,0 +1,40 @@ +--WINDOW. +width = 800 +height = 640 +fullscreen = 0 + +-- SCREEN. +fps = 0 + +-- JOYSTICK. +-- Can be number or substring of joystick name. +joystick = "Precision" + +-- KEYBINDINGS. +-- Type can be keyboard, jaxis or jbutton. +-- +-- If left is an axis, it will automatically set right to the same axis. +-- setting both to the same axis (key). +-- You can use reverse = 1 option to reverse them. + +-- Movement. +accel = { type = "jbutton", key = 0 } +left = { type = "jaxis", key = 0 } +right = { type = "jaxis", key = 0 } + +-- Combat. +primary = { type = "jbutton", key = 1 } +target = { type = "jbutton", key = 4 } +target_nearest = { type = "jbutton", key = 3 } +face = { type = "keyboard", key = 38 } +board = { type = "keyboard", key = 57 } +secondary = { type = "jbutton", key = 7 } +secondary_next = { type = "jbutotn", key = 5 } + +-- Space. + +-- Gui. +mapzoomin = { type = "jbutton", key = 4 } +mapzoomout = { type = "jbuton", key = 6 } +screenshot = { type = "keyboard", key = 82 } + diff --git a/dat/outfit.xml b/dat/outfit.xml index baae407..a527833 100644 --- a/dat/outfit.xml +++ b/dat/outfit.xml @@ -13,7 +13,7 @@ 300 30 - 20 + 13 10 diff --git a/src/conf.c b/src/conf.c index df46c00..17dd719 100644 --- a/src/conf.c +++ b/src/conf.c @@ -200,3 +200,9 @@ void conf_parseCLI(int argc, char** argv) { } } +// Saves the current configuration. +int conf_saveConfig(void) { + // TODO: + return 0; +} + diff --git a/src/conf.h b/src/conf.h index cfd18bf..b081c4a 100644 --- a/src/conf.h +++ b/src/conf.h @@ -1,6 +1,10 @@ #pragma once +// Loading. void conf_setDefaults(void); int conf_loadConfig(const char* file); void conf_parseCLI(int argc, char** argv); +// Saving. +int conf_saveConfig(void); + diff --git a/src/input.c b/src/input.c index 5e77aa0..8fdcb33 100644 --- a/src/input.c +++ b/src/input.c @@ -1,6 +1,7 @@ #include "main.h" #include "log.h" #include "player.h" +#include "pause.h" #include "input.h" #define KEY_PRESS ( 1.) @@ -22,7 +23,7 @@ const char* keybindNames[] = { "accel", "left", "right", // Movement. "primary", "target", "target_nearest", "face", "board", // Combat. "secondary", "secondary_next", // Secondary weapons. "target_planet", "land", // Navigation. - "mapzoomin", "mapzoomout", "screenshot", "end" }; // Misc. + "mapzoomin", "mapzoomout", "screenshot", "pause", "end" }; // Misc. // From player.c extern double player_turn; extern double player_acc; @@ -52,6 +53,7 @@ void input_setDefault(void) { input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0); input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0); input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_F12, 0); + input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_p, 0); } // Initialization/exit functions (does not assign keys). @@ -151,24 +153,34 @@ static void input_key(int keynum, double value, int abs) { } } // Board those ships. - else if(KEY("board")) { + else if(KEY("board") && !paused) { if(value == KEY_PRESS) player_board(); } - // Shooting secondary weapon. + // Selecting secondary weapon. + else if(KEY("secondary") && !paused) { + if(value == KEY_PRESS) player_setFlag(PLAYER_SECONDARY); + else if(value == KEY_RELEASE) player_rmFlag(PLAYER_SECONDARY); + } + // Selecting secondary weapon. + else if(KEY("secondary_next") && !paused) { + if(value == KEY_PRESS) player_secondaryNext(); + } + // Selecting secondary weapon. else if(KEY("secondary")) { if(value == KEY_PRESS) player_setFlag(PLAYER_SECONDARY); else if(value == KEY_RELEASE) player_rmFlag(PLAYER_SECONDARY); } // Selecting secondary weapon. - else if(KEY("secondary_next")) { + else if(KEY("secondary_next") && !paused) { if(value == KEY_PRESS) player_secondaryNext(); } + // Space. // Target planet (cycles just like target). - else if(KEY("target_planet")) { + else if(KEY("target_planet") && !paused) { if(value == KEY_PRESS) player_targetPlanet(); } // Target nearest planet or attempt to land. - else if(KEY("land")) { + else if(KEY("land") && !paused) { if(value == KEY_PRESS) player_land(); } // Zoom in. @@ -180,9 +192,15 @@ static void input_key(int keynum, double value, int abs) { if(value == KEY_PRESS) player_setRadarRel(-1); } // Take a screenshot. - else if(KEY("screenshot")) { + if(KEY("screenshot")) { if(value == KEY_PRESS) player_screenshot(); } + // Pause the game. + if(KEY("pause")) { + if(value == KEY_PRESS) { + if(paused) unpause(); + } else pause(); + } } // --Events-- diff --git a/src/main.c b/src/main.c index 01e910a..4076c93 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,8 @@ #include "weapon.h" #include "faction.h" #include "xml.h" +#include "pause.h" +#include "toolkit.h" #include "pilot.h" #define XML_START_ID "Start" @@ -30,9 +32,8 @@ #define MINIMUM_FPS 0.5 #define FONT_SIZE 12 -int toolkit = 0; // Toolkit has a window open. static int quit = 0; // Primary loop. -static unsigned int time = 0; // Calculate FPS and movement. +unsigned int time = 0; // Calculate FPS and movement. static char version[VERSION_LEN]; // Just some default crap. @@ -54,7 +55,6 @@ static void update_space(void); static void render_space(void); int main(int argc, char** argv) { - // Print the version. snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV); LOG(" "APPNAME" v%s", version); @@ -123,6 +123,7 @@ int main(int argc, char** argv) { // Misc openGL init stuff. gl_fontInit(NULL, NULL, FONT_SIZE); // Init default font size. gui_init(); // Init the GUI crap. + toolkit_init(); // Init the toolkit. // Data loading. factions_load(); @@ -149,15 +150,14 @@ int main(int argc, char** argv) { input_handle(&event); // handles all the events the player keybinds. } - if(toolkit) { + glClear(GL_COLOR_BUFFER_BIT); - } else { - // Player is flying around. - update_space(); - glClear(GL_COLOR_BUFFER_BIT); - render_space(); - SDL_GL_SwapBuffers(); - } + if(!paused) update_space(); // Update the game. + + render_space(); + if(toolkit) toolkit_render(); + + SDL_GL_SwapBuffers(); } // Unload data. @@ -173,6 +173,7 @@ int main(int argc, char** argv) { gl_freeFont(NULL); // Exit subsystems. + toolkit_exit(); // Kill the toolkit. ai_exit(); // Stop the Lua AI magicness. joystick_exit(); // Release joystick. input_exit(); // Clean up keybindings. diff --git a/src/pause.c b/src/pause.c new file mode 100644 index 0000000..8ff1b6d --- /dev/null +++ b/src/pause.c @@ -0,0 +1,61 @@ +#include "weapon.h" +#include "pilot.h" +#include "pause.h" + +// Main thing with pausing is to allow things based on time to +// work properly when the toolkit opens a window. + +int paused = 0; // Are we paused. + +// From pilot.c +extern Pilot** pilot_stack; +extern int pilots; +// From main.c +extern unsigned int time; + +static void pilots_pause(void); +static void pilots_unpause(void); + +// Pause the game. +void pause(void) { + if(paused) return; // Well well.. We are paused already. + + time -= SDL_GetTicks(); + pilots_pause(); + weapons_pause(); + + paused = 0; // We should unpause it. +} + +void unpause(void) { + if(!paused) return; // We are unpaused already. + + time += SDL_GetTicks(); + pilots_unpause(); + weapons_unpause(); + + paused = 0; +} + +static void pilots_pause(void) { + int i, j; + unsigned int t = SDL_GetTicks(); + for(i = 0; i < pilots; i++) { + pilot_stack[i]->tcontrol -= t; + + for(j = 0; j < MAX_AI_TIMERS; j++) + pilot_stack[i]->timer[j] -= t; + } +} + +static void pilots_unpause(void) { + int i, j; + unsigned int t = SDL_GetTicks(); + for(i = 0; i < pilots; i++) { + pilot_stack[i]->tcontrol += t; + + for(j = 0; j < MAX_AI_TIMERS; j++) + pilot_stack[i]->timer[j] += t; + } +} + diff --git a/src/pause.h b/src/pause.h new file mode 100644 index 0000000..92c29f9 --- /dev/null +++ b/src/pause.h @@ -0,0 +1,7 @@ +#pragma once + +extern int paused; + +void pause(void); +void unpause(void); + diff --git a/src/space.c b/src/space.c index 0403cd8..54ec5df 100644 --- a/src/space.c +++ b/src/space.c @@ -8,6 +8,7 @@ #include "space.h" #include "faction.h" #include "xml.h" +#include "pause.h" #include "player.h" #define XML_PLANET_ID "Planets" @@ -440,14 +441,16 @@ void space_render(double dt) { glTranslated(-(double)gl_screen.w/2., -(double)gl_screen.h/2., 0.); glBegin(GL_POINTS); for(i = 0; i < nstars; i++) { - // Update the position. - stars[i].x -= VX(player->solid->vel)/(15.-10.*stars[i].brightness)*dt; - stars[i].y -= VY(player->solid->vel)/(15.-10.*stars[i].brightness)*dt; - // Scroll those stars bitch! - if(stars[i].x > gl_screen.w + STAR_BUF) stars[i].x = -STAR_BUF; - else if(stars[i].x < -STAR_BUF) stars[i].x = gl_screen.w + STAR_BUF; - if(stars[i].y > gl_screen.h + STAR_BUF) stars[i].y = -STAR_BUF; - else if(stars[i].y < -STAR_BUF) stars[i].y = gl_screen.h + STAR_BUF; + if(!paused) { + // Update the position. + stars[i].x -= VX(player->solid->vel)/(15.-10.*stars[i].brightness)*dt; + stars[i].y -= VY(player->solid->vel)/(15.-10.*stars[i].brightness)*dt; + // Scroll those stars bitch! + if(stars[i].x > gl_screen.w + STAR_BUF) stars[i].x = -STAR_BUF; + else if(stars[i].x < -STAR_BUF) stars[i].x = gl_screen.w + STAR_BUF; + if(stars[i].y > gl_screen.h + STAR_BUF) stars[i].y = -STAR_BUF; + else if(stars[i].y < -STAR_BUF) stars[i].y = gl_screen.h + STAR_BUF; + } // Render. glColor4d(1., 1., 1., stars[i].brightness); glVertex2d(stars[i].x, stars[i].y); diff --git a/src/toolkit.c b/src/toolkit.c new file mode 100644 index 0000000..44bbf9f --- /dev/null +++ b/src/toolkit.c @@ -0,0 +1,91 @@ +#include "log.h" +#include "pause.h" +#include "toolkit.h" + +typedef struct { + unsigned int id; // Unique identifier. + + double x,y; // Position. + double w,h; // Dimensions. + + gl_texture* t; // Possible texture. +} Window; + +static unsigned int genwid = 0; // Generate unique id. + +int toolkit = 0; + +#define MIN_WINDOWS 3 +static Window** windows = NULL; +static int nwindows = 0; +static int mwindows = 0; + +// Create a window. +unsigned int window_create(int x, int y, int w, int h, gl_texture* t) { + Window* wtmp = NULL; + if(nwindows == mwindows) { + // We have reached the memory limit. + windows = realloc(windows, sizeof(Window*)*(++mwindows)); + if(windows == NULL) WARN("Out of memory"); + } + wtmp = malloc(sizeof(Window)); + if(wtmp == NULL) WARN("Out of memory"); + + int wid = (++genwid); // Unique id + + wtmp->id = wid; + + wtmp->x = x; + wtmp->y = y; + wtmp->h = h; + wtmp->w = w; + wtmp->t = t; + + windows[nwindows++] = wtmp; + + if(toolkit == 0) toolkit = 1; // Enable the toolkit. + + return wid; +} + +// Destroy a window. +void window_destroy(unsigned int wid) { + int i; + + // Destroy the window. + for(i = 0; i < nwindows; i++) + if(windows[i]->id == wid) { + free(windows[i]); + break; + } + // Move the other windows down a layer. + for(; i<(nwindows-1); i++) + windows[i] = windows[i+1]; + + nwindows--; + if(nwindows == 0) toolkit = 0; // Disable the toolkit. +} + +// Render the window. +void toolkit_render(void) { + +} + +// Init. +int toolkit_init(void) { + windows = malloc(sizeof(Window)*MIN_WINDOWS); + nwindows = 0; + mwindows = MIN_WINDOWS; + + return 0; +} + +// Exit the toolkit. +void toolkit_exit(void) { + int i; + for(i = 0; i < nwindows; i++) { + window_destroy(windows[i]->id); + free(windows); + } +} + diff --git a/src/toolkit.h b/src/toolkit.h new file mode 100644 index 0000000..ba55485 --- /dev/null +++ b/src/toolkit.h @@ -0,0 +1,18 @@ +#pragma once +#include "opengl.h" + +extern int toolkit; + +// Creation. +unsigned int window_create(int x, int y, int w, int h, gl_texture* t); + +// Destroy window. +void window_destroy(unsigned int wid); + +// Render. +void toolkit_render(void); + +// Init/Exit. +int toolkit_init(void); +void toolkit_exit(void); + diff --git a/src/weapon.c b/src/weapon.c index 9ead979..f96c681 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -80,6 +80,26 @@ void weapon_minimap(const double res, const double w, const double h, const Rada } #undef PIXEL +// Pause/Unpause the weapon system. +void weapons_pause(void) { + int i; + unsigned int t = SDL_GetTicks(); + for(i = 0; i < nwbackLayer; i++) + wbackLayer[i]->timer -= t; + for(i = 0; i < nwfrontLayer; i++) + wfrontLayer[i]->timer -= t; +} + + +void weapons_unpause(void) { + int i; + unsigned int t = SDL_GetTicks(); + for(i = 0; i < nwbackLayer; i++) + wbackLayer[i]->timer += t; + for(i = 0; i < nwfrontLayer; i++) + wfrontLayer[i]->timer += t; +} + static void think_seeker(Weapon* w) { if(w->target == w->parent) return; // HEY! Self harm is not allowed. diff --git a/src/weapon.h b/src/weapon.h index 0fe147d..9356d79 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -8,6 +8,10 @@ void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel, unsigned int parent, const unsigned int target, const WeaponLayer layer); +// Pausing. +void weapons_pause(void); +void weapons_unpause(void); + // Update. void weapons_update(const double dt); void weapons_render(const WeaponLayer layer);