[Add] Keybord events work with lists. I think I optimized pilots to use temp sprite values here too. Forgot to diff.
This commit is contained in:
parent
9ac872aac4
commit
8881dc5265
4
README
4
README
@ -109,8 +109,8 @@ Keys:
|
|||||||
-- 'j' : Jump through hyperspace.
|
-- 'j' : Jump through hyperspace.
|
||||||
|
|
||||||
GUI:
|
GUI:
|
||||||
-- UP : Zoom in.
|
-- '0' : Zoom in.
|
||||||
-- DOWN : Zoom out.
|
-- '9' : Zoom out.
|
||||||
-- F12 : Take a screenshot.
|
-- F12 : Take a screenshot.
|
||||||
-- ESC : Menu.
|
-- ESC : Menu.
|
||||||
-- 'i' : Opens the character information window.
|
-- 'i' : Opens the character information window.
|
||||||
|
@ -61,8 +61,8 @@ void input_setDefault(void) {
|
|||||||
input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, 0);
|
input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, 0);
|
||||||
|
|
||||||
// Misc.
|
// Misc.
|
||||||
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
|
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_0, 0);
|
||||||
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
|
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_9, 0);
|
||||||
input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_F12, 0);
|
input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_F12, 0);
|
||||||
input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, 0);
|
input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, 0);
|
||||||
input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, 0);
|
input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, 0);
|
||||||
|
@ -189,6 +189,7 @@ int main(int argc, char** argv) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
fps_control(); // Who doesn't love FPS control?
|
fps_control(); // Who doesn't love FPS control?
|
||||||
|
toolkit_update(); // Simulate key repetition.
|
||||||
if(!paused && !toolkit) update_space(); // Update the game.
|
if(!paused && !toolkit) update_space(); // Update the game.
|
||||||
|
|
||||||
render_space();
|
render_space();
|
||||||
|
12
src/pilot.c
12
src/pilot.c
@ -260,12 +260,9 @@ void pilot_setAmmo(Pilot* p) {
|
|||||||
|
|
||||||
// Render the pilot.
|
// Render the pilot.
|
||||||
void pilot_render(Pilot* p) {
|
void pilot_render(Pilot* p) {
|
||||||
int sx, sy;
|
gl_blitSprite(p->ship->gfx_space,
|
||||||
|
p->solid->pos.x, p->solid->pos.y,
|
||||||
// Get the sprite corresponding to the direction facing.
|
p->tsx, p->tsy, NULL);
|
||||||
gl_getSpriteFromDir(&sx, &sy, p->ship->gfx_space, p->solid->dir);
|
|
||||||
|
|
||||||
gl_blitSprite(p->ship->gfx_space, p->solid->pos.x, p->solid->pos.y, sx, sy, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the pilot.
|
// Update the pilot.
|
||||||
@ -320,6 +317,8 @@ static void pilot_update(Pilot* pilot, const double dt) {
|
|||||||
|
|
||||||
// Update the solid.
|
// Update the solid.
|
||||||
pilot->solid->update(pilot->solid, dt);
|
pilot->solid->update(pilot->solid, dt);
|
||||||
|
gl_getSpriteFromDir(&pilot->tsx, &pilot->tsy,
|
||||||
|
pilot->ship->gfx_space, pilot->solid->dir);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// We are still alive.
|
// We are still alive.
|
||||||
@ -333,6 +332,7 @@ static void pilot_update(Pilot* pilot, const double dt) {
|
|||||||
|
|
||||||
// Update the solid.
|
// Update the solid.
|
||||||
(*pilot->solid->update)(pilot->solid, dt);
|
(*pilot->solid->update)(pilot->solid, dt);
|
||||||
|
gl_getSpriteFromDir(&pilot->tsx, &pilot->tsy, pilot->ship->gfx_space, pilot->solid->dir);
|
||||||
|
|
||||||
if(!pilot_isFlag(pilot, PILOT_HYPERSPACE))
|
if(!pilot_isFlag(pilot, PILOT_HYPERSPACE))
|
||||||
// Should not go faster.
|
// Should not go faster.
|
||||||
|
@ -58,6 +58,7 @@ typedef struct Pilot_ {
|
|||||||
// Object characteristics.
|
// Object characteristics.
|
||||||
Ship* ship; // Pilots ship.
|
Ship* ship; // Pilots ship.
|
||||||
Solid* solid; // Associated solid (physics).
|
Solid* solid; // Associated solid (physics).
|
||||||
|
int tsx, tsy; // Current sprite, calculated on update.
|
||||||
|
|
||||||
// Current health.
|
// Current health.
|
||||||
double armour, shield, energy;
|
double armour, shield, energy;
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
|
#include "lephisto.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "pause.h"
|
#include "pause.h"
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
#include "toolkit.h"
|
#include "toolkit.h"
|
||||||
|
|
||||||
|
#define INPUT_DELAY 500
|
||||||
|
#define INPUT_FREQ 100
|
||||||
|
|
||||||
typedef enum WidgetType_ {
|
typedef enum WidgetType_ {
|
||||||
WIDGET_NULL,
|
WIDGET_NULL,
|
||||||
WIDGET_BUTTON,
|
WIDGET_BUTTON,
|
||||||
@ -85,6 +89,8 @@ static void toolkit_mouseEvent(SDL_Event* event);
|
|||||||
static int toolkit_keyEvent(SDL_Event* event);
|
static int toolkit_keyEvent(SDL_Event* event);
|
||||||
static void toolkit_nextFocus(void);
|
static void toolkit_nextFocus(void);
|
||||||
static void toolkit_triggerFocus(void);
|
static void toolkit_triggerFocus(void);
|
||||||
|
static Widget* toolkit_getFocus(void);
|
||||||
|
static void toolkit_listScroll(Widget* wgt, int direction);
|
||||||
// Render.
|
// Render.
|
||||||
static void window_render(Window* w);
|
static void window_render(Window* w);
|
||||||
static void toolkit_renderButton(Widget* btn, double bx, double by);
|
static void toolkit_renderButton(Widget* btn, double bx, double by);
|
||||||
@ -690,7 +696,7 @@ static void toolkit_renderList(Widget* lst, double bx, double by) {
|
|||||||
toolkit_drawOutline(x, y, lst->w, lst->h, 1., oc, NULL);
|
toolkit_drawOutline(x, y, lst->w, lst->h, 1., oc, NULL);
|
||||||
|
|
||||||
// Draw selected.
|
// Draw selected.
|
||||||
toolkit_drawRect(x, y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+3.),
|
toolkit_drawRect(x, y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.),
|
||||||
lst->w, gl_defFont.h+2., &cHilight, NULL);
|
lst->w, gl_defFont.h+2., &cHilight, NULL);
|
||||||
|
|
||||||
// Draw content.
|
// Draw content.
|
||||||
@ -780,6 +786,7 @@ static void toolkit_mouseEvent(SDL_Event* event) {
|
|||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
wgt->status = WIDGET_STATUS_MOUSEDOWN;
|
||||||
|
w->focus = i;
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
|
if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
|
||||||
@ -800,6 +807,25 @@ static void toolkit_mouseEvent(SDL_Event* event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle the key events.
|
// Handle the key events.
|
||||||
|
static SDLKey input_key;
|
||||||
|
static unsigned int input_keyTime;
|
||||||
|
static int input_keyCounter;
|
||||||
|
static void toolkit_regKey(SDLKey key) {
|
||||||
|
if((input_key == 0) && (input_keyTime == 0)) {
|
||||||
|
input_key = key;
|
||||||
|
input_keyTime = SDL_GetTicks();
|
||||||
|
input_keyCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void toolkit_unregKey(SDLKey key) {
|
||||||
|
if(input_key == key) {
|
||||||
|
input_key = 0;
|
||||||
|
input_keyTime = 0;
|
||||||
|
input_keyCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int toolkit_keyEvent(SDL_Event* event) {
|
static int toolkit_keyEvent(SDL_Event* event) {
|
||||||
SDLKey key = event->key.keysym.sym;
|
SDLKey key = event->key.keysym.sym;
|
||||||
switch(key) {
|
switch(key) {
|
||||||
@ -812,14 +838,51 @@ static int toolkit_keyEvent(SDL_Event* event) {
|
|||||||
toolkit_triggerFocus();
|
toolkit_triggerFocus();
|
||||||
return 1;
|
return 1;
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
|
if(event->type == SDL_KEYDOWN) {
|
||||||
|
toolkit_regKey(SDLK_UP);
|
||||||
|
toolkit_listScroll(toolkit_getFocus(), +1);
|
||||||
|
}
|
||||||
|
else if(event->type == SDL_KEYUP)
|
||||||
|
toolkit_unregKey(SDLK_UP);
|
||||||
|
printf("Key Up\n");
|
||||||
|
return 0;
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
// TODO: list up/down.
|
if(event->type == SDL_KEYDOWN) {
|
||||||
|
toolkit_regKey(SDLK_DOWN);
|
||||||
|
toolkit_listScroll(toolkit_getFocus(), -1);
|
||||||
|
}
|
||||||
|
else if(event->type == SDL_KEYUP)
|
||||||
|
toolkit_unregKey(SDLK_DOWN);
|
||||||
|
printf("Key Down\n");
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void toolkit_update(void) {
|
||||||
|
unsigned int t;
|
||||||
|
|
||||||
|
t = SDL_GetTicks();
|
||||||
|
|
||||||
|
if(input_key == 0) return;
|
||||||
|
|
||||||
|
if(input_keyTime + INPUT_DELAY + input_keyCounter*INPUT_FREQ > t)
|
||||||
|
return;
|
||||||
|
|
||||||
|
input_keyCounter++;
|
||||||
|
switch(input_key) {
|
||||||
|
case SDLK_UP:
|
||||||
|
toolkit_listScroll(toolkit_getFocus(), +1);
|
||||||
|
break;
|
||||||
|
case SDLK_DOWN:
|
||||||
|
toolkit_listScroll(toolkit_getFocus(), -1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Focus next widget.
|
// Focus next widget.
|
||||||
static void toolkit_nextFocus(void) {
|
static void toolkit_nextFocus(void) {
|
||||||
Window* wdw = &windows[nwindows-1]; // Get active window.
|
Window* wdw = &windows[nwindows-1]; // Get active window.
|
||||||
@ -854,6 +917,31 @@ static void toolkit_triggerFocus(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to scroll up/down by direction.
|
||||||
|
static void toolkit_listScroll(Widget* wgt, int direction) {
|
||||||
|
if(wgt == NULL) return;
|
||||||
|
|
||||||
|
switch(wgt->type) {
|
||||||
|
case WIDGET_LIST:
|
||||||
|
wgt->dat.lst.selected -= direction;
|
||||||
|
wgt->dat.lst.selected = MAX(0, wgt->dat.lst.selected);
|
||||||
|
wgt->dat.lst.selected = MIN(wgt->dat.lst.selected, wgt->dat.lst.noptions-1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the focused widget.
|
||||||
|
static Widget* toolkit_getFocus(void) {
|
||||||
|
Window* wdw;
|
||||||
|
wdw = &windows[nwindows-1];
|
||||||
|
|
||||||
|
if(wdw->focus == -1) return NULL;
|
||||||
|
|
||||||
|
return &wdw->widgets[wdw->focus];
|
||||||
|
}
|
||||||
|
|
||||||
// Init.
|
// Init.
|
||||||
int toolkit_init(void) {
|
int toolkit_init(void) {
|
||||||
windows = malloc(sizeof(Window)*MIN_WINDOWS);
|
windows = malloc(sizeof(Window)*MIN_WINDOWS);
|
||||||
|
@ -41,6 +41,7 @@ void toolkit_render(void);
|
|||||||
|
|
||||||
// Input.
|
// Input.
|
||||||
int toolkit_input(SDL_Event* event);
|
int toolkit_input(SDL_Event* event);
|
||||||
|
void toolkit_update(void);
|
||||||
|
|
||||||
// Init/Exit.
|
// Init/Exit.
|
||||||
int toolkit_init(void);
|
int toolkit_init(void);
|
||||||
|
@ -267,8 +267,9 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
|
|||||||
gl_getSpriteFromDir(&wsx, &wsy, gfx, w->solid->dir);
|
gl_getSpriteFromDir(&wsx, &wsy, gfx, w->solid->dir);
|
||||||
|
|
||||||
for(i = 0; i < pilots; i++) {
|
for(i = 0; i < pilots; i++) {
|
||||||
gl_getSpriteFromDir(&psx, &psy, pilot_stack[i]->ship->gfx_space,
|
psx = pilot_stack[i]->tsx;
|
||||||
pilot_stack[i]->solid->dir);
|
psy = pilot_stack[i]->tsy;
|
||||||
|
|
||||||
if(w->parent == pilot_stack[i]->id) continue; // Hey! That's you.
|
if(w->parent == pilot_stack[i]->id) continue; // Hey! That's you.
|
||||||
|
|
||||||
if((weapon_isSmart(w)) && (pilot_stack[i]->id == w->target) &&
|
if((weapon_isSmart(w)) && (pilot_stack[i]->id == w->target) &&
|
||||||
|
Loading…
Reference in New Issue
Block a user