Start of mouse control integration.

This commit is contained in:
Steve 2015-11-23 14:52:11 +00:00
parent b8409765d3
commit 95e26f3e7b
17 changed files with 286 additions and 73 deletions

View File

@ -24,7 +24,7 @@ OBJS += effects.o entities.o extractionPoint.o
OBJS += fighters.o OBJS += fighters.o
OBJS += galacticMap.o game.o grid.o OBJS += galacticMap.o game.o grid.o
OBJS += hud.o OBJS += hud.o
OBJS += init.o io.o items.o OBJS += init.o input.o io.o items.o
OBJS += load.o lookup.o OBJS += load.o lookup.o
OBJS += main.o mission.o missionInfo.o OBJS += main.o mission.o missionInfo.o
OBJS += objectives.o options.o OBJS += objectives.o options.o

BIN
gfx/input/mousePointer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 B

View File

@ -28,6 +28,9 @@ static void initPlayerSelect(void);
static void activateBoost(void); static void activateBoost(void);
static void deactivateBoost(void); static void deactivateBoost(void);
static void activateECM(void); static void activateECM(void);
static void handleKeyboard(void);
static void faceMouse(void);
static void handleMouse(void);
static int selectedPlayerIndex; static int selectedPlayerIndex;
static int availableGuns[BT_MAX]; static int availableGuns[BT_MAX];
@ -78,6 +81,39 @@ void doPlayer(void)
self = player; self = player;
if (player->alive == ALIVE_ALIVE) if (player->alive == ALIVE_ALIVE)
{
handleKeyboard();
handleMouse();
}
player->angle = ((int)player->angle) % 360;
if (player->health <= 0 && battle.status == MS_IN_PROGRESS)
{
if (!battle.epic)
{
failMission();
}
else if (player->health == -FPS)
{
initPlayerSelect();
}
}
if (battle.status == MS_IN_PROGRESS)
{
selectMissionTarget();
}
}
if (battle.boostTimer == (int)BOOST_FINISHED_TIME)
{
deactivateBoost();
}
}
static void handleKeyboard(void)
{ {
if (app.keyboard[SDL_SCANCODE_LEFT]) if (app.keyboard[SDL_SCANCODE_LEFT])
{ {
@ -145,32 +181,66 @@ void doPlayer(void)
{ {
activateECM(); activateECM();
} }
}
}
if (!battle.missionTarget) static void handleMouse(void)
{ {
selectMissionTarget(); faceMouse();
if (battle.status == MS_IN_PROGRESS)
{
if (app.mouse.button[SDL_BUTTON_LEFT] && !player->reload && player->guns[0].type)
{
fireGuns(player);
}
if (app.mouse.button[SDL_BUTTON_RIGHT])
{
if (battle.boostTimer > BOOST_FINISHED_TIME)
{
applyFighterThrust();
}
}
else
{
applyFighterBrakes();
}
if (app.mouse.button[SDL_BUTTON_MIDDLE] && player->missiles && player->target)
{
if (getDistance(player->x, player->y, player->target->x, player->target->y) <= SCREEN_WIDTH)
{
fireMissile(player);
}
else
{
addHudMessage(colors.white, "Target not in range");
}
app.mouse.button[SDL_BUTTON_MIDDLE] = 0;
} }
} }
} }
player->angle = ((int)player->angle) % 360; static void faceMouse(void)
{
int dir;
int x, y, wantedAngle;
if (player->health <= 0 && battle.status == MS_IN_PROGRESS) x = player->x - battle.camera.x;
{ y = player->y - battle.camera.y;
if (!battle.epic) wantedAngle = getAngle(x, y, app.mouse.x, app.mouse.y);
{
failMission();
}
else if (player->health == -FPS)
{
initPlayerSelect();
}
}
}
if (battle.boostTimer == (int)BOOST_FINISHED_TIME) wantedAngle %= 360;
if (fabs(wantedAngle - player->angle) > 2)
{ {
deactivateBoost(); dir = ((int)(wantedAngle - player->angle + 360)) % 360 > 180 ? -1 : 1;
player->angle += dir * 4;
player->angle = mod(player->angle, 360);
} }
} }
@ -305,6 +375,7 @@ static void selectTarget(void)
Entity *targets[MAX_SELECTABLE_TARGETS]; Entity *targets[MAX_SELECTABLE_TARGETS];
i = 0; i = 0;
near = NULL;
memset(targets, 0, sizeof(Entity*) * MAX_SELECTABLE_TARGETS); memset(targets, 0, sizeof(Entity*) * MAX_SELECTABLE_TARGETS);
for (e = battle.entityHead.next ; e != NULL ; e = e->next) for (e = battle.entityHead.next ; e != NULL ; e = e->next)

View File

@ -35,6 +35,7 @@ extern void addHudMessage(SDL_Color c, char *format, ...);
extern int mod(int n, int x); extern int mod(int n, int x);
extern void playSound(int id); extern void playSound(int id);
extern void failMission(void); extern void failMission(void);
extern float getAngle(int x1, int y1, int x2, int y2);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern App app; extern App app;

View File

@ -33,6 +33,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define SCREEN_HEIGHT 720 #define SCREEN_HEIGHT 720
#define MAX_KEYBOARD_KEYS 350 #define MAX_KEYBOARD_KEYS 350
#define MAX_MOUSE_BUTTONS 8
#define FPS 60 #define FPS 60
#define LOGIC_RATE (1000 / FPS) #define LOGIC_RATE (1000 / FPS)

View File

@ -50,6 +50,8 @@ void presentScene(void)
drawText(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 25, 14, TA_CENTER, colors.white, "FPS: %d", app.fps); drawText(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 25, 14, TA_CENTER, colors.white, "FPS: %d", app.fps);
#endif #endif
drawMouse();
SDL_SetRenderTarget(app.renderer, NULL); SDL_SetRenderTarget(app.renderer, NULL);
SDL_RenderCopy(app.renderer, app.backBuffer, NULL, NULL); SDL_RenderCopy(app.renderer, app.backBuffer, NULL, NULL);
SDL_RenderPresent(app.renderer); SDL_RenderPresent(app.renderer);

View File

@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../structs.h" #include "../structs.h"
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void drawMouse(void);
extern App app; extern App app;
extern Colors colors; extern Colors colors;

View File

@ -90,6 +90,8 @@ void initTitle(void)
endSectionTransition(); endSectionTransition();
SDL_WarpMouseInWindow(app.window, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 100);
playMusic("music/Rise of spirit.ogg"); playMusic("music/Rise of spirit.ogg");
} }

View File

@ -62,6 +62,18 @@ int main(int argc, char *argv[])
{ {
switch (event.type) switch (event.type)
{ {
case SDL_MOUSEBUTTONDOWN:
doMouseDown(&event.button);
break;
case SDL_MOUSEBUTTONUP:
doMouseUp(&event.button);
break;
case SDL_MOUSEMOTION:
doMouseMove(&event.motion);
break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.scancode >= 0 && event.key.keysym.scancode < MAX_KEYBOARD_KEYS) if (event.key.keysym.scancode >= 0 && event.key.keysym.scancode < MAX_KEYBOARD_KEYS)
{ {

View File

@ -31,6 +31,9 @@ extern void initGameSystem(void);
extern void initTitle(void); extern void initTitle(void);
extern void loadTestMission(char *filename); extern void loadTestMission(char *filename);
extern void saveScreenshot(void); extern void saveScreenshot(void);
extern void doMouseDown(SDL_MouseButtonEvent *event);
extern void doMouseUp(SDL_MouseButtonEvent *event);
extern void doMouseMove(SDL_MouseMotionEvent *event);
App app; App app;
Colors colors; Colors colors;

View File

@ -290,6 +290,12 @@ struct HudMessage {
HudMessage *next; HudMessage *next;
}; };
typedef struct {
int x;
int y;
int button[MAX_MOUSE_BUTTONS];
} Mouse;
typedef struct { typedef struct {
char saveDir[MAX_FILENAME_LENGTH]; char saveDir[MAX_FILENAME_LENGTH];
int winWidth; int winWidth;
@ -301,6 +307,7 @@ typedef struct {
int soundVolume; int soundVolume;
int vSync; int vSync;
int fps; int fps;
Mouse mouse;
int keyboard[MAX_KEYBOARD_KEYS]; int keyboard[MAX_KEYBOARD_KEYS];
SDL_Texture *backBuffer; SDL_Texture *backBuffer;
SDL_Renderer *renderer; SDL_Renderer *renderer;

View File

@ -138,6 +138,8 @@ void initGameSystem(void)
initColor(&colors.lightGrey, 192, 192, 192); initColor(&colors.lightGrey, 192, 192, 192);
initColor(&colors.darkGrey, 128, 128, 128); initColor(&colors.darkGrey, 128, 128, 128);
initInput();
initLookups(); initLookups();
initFonts(); initFonts();

View File

@ -61,6 +61,7 @@ extern void destroyTextures(void);
extern void destroyGalacticMap(void); extern void destroyGalacticMap(void);
extern void destroyWidgets(void); extern void destroyWidgets(void);
extern void expireTexts(void); extern void expireTexts(void);
extern void initInput(void);
extern App app; extern App app;
extern Colors colors; extern Colors colors;

57
src/system/input.c Normal file
View File

@ -0,0 +1,57 @@
/*
Copyright (C) 2015 Parallel Realities
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "input.h"
static SDL_Texture *mousePointer;
void initInput(void)
{
memset(&app.mouse, 0, sizeof(Mouse));
mousePointer = getTexture("gfx/input/mousePointer.png");
}
void doMouseDown(SDL_MouseButtonEvent *event)
{
app.mouse.button[event->button] = 1;
}
void doMouseUp(SDL_MouseButtonEvent *event)
{
app.mouse.button[event->button] = 0;
}
void doMouseMove(SDL_MouseMotionEvent *event)
{
}
void drawMouse(void)
{
int x, y;
SDL_GetMouseState(&x, &y);
app.mouse.x = x * app.scaleX;
app.mouse.y = y * app.scaleY;
blit(mousePointer, app.mouse.x, app.mouse.y, 1);
}

29
src/system/input.h Normal file
View File

@ -0,0 +1,29 @@
/*
Copyright (C) 2015 Parallel Realities
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "SDL2/SDL.h"
#include "../defs.h"
#include "../structs.h"
extern SDL_Texture *getTexture(char *filename);
extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern App app;

View File

@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void loadWidgets(char *filename); static void loadWidgets(char *filename);
static void loadWidgetSet(char *filename); static void loadWidgetSet(char *filename);
static void handleKeyboard(void); static void handleKeyboard(void);
static void handleMouse(void);
static void createOptions(Widget *w, char *options); static void createOptions(Widget *w, char *options);
static Widget head; static Widget head;
@ -53,6 +54,8 @@ void doWidgets(void)
if (drawingWidgets) if (drawingWidgets)
{ {
handleKeyboard(); handleKeyboard();
handleMouse();
} }
drawingWidgets = 0; drawingWidgets = 0;
@ -82,6 +85,7 @@ void selectWidget(const char *name, const char *group)
void drawWidgets(const char *group) void drawWidgets(const char *group)
{ {
int mouseOver;
Widget *w; Widget *w;
drawingWidgets = 1; drawingWidgets = 1;
@ -90,6 +94,14 @@ void drawWidgets(const char *group)
{ {
if (w->visible && strcmp(w->group, group) == 0) if (w->visible && strcmp(w->group, group) == 0)
{ {
mouseOver = (w->enabled && collision(w->rect.x, w->rect.y, w->rect.w, w->rect.h, app.mouse.x, app.mouse.y, 1, 1));
if (mouseOver && selectedWidget != w)
{
playSound(SND_GUI_CLICK);
selectedWidget = w;
}
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &w->rect); SDL_RenderFillRect(app.renderer, &w->rect);
@ -261,13 +273,24 @@ static void handleKeyboard(void)
if (app.keyboard[SDL_SCANCODE_RETURN] && selectedWidget->action) if (app.keyboard[SDL_SCANCODE_RETURN] && selectedWidget->action)
{ {
playSound(SND_GUI_SELECT); playSound(SND_GUI_SELECT);
selectedWidget->action(); selectedWidget->action();
} }
memset(app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS); memset(app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
} }
static void handleMouse(void)
{
if (selectedWidget && selectedWidget->action && app.mouse.button[SDL_BUTTON_LEFT])
{
if (collision(selectedWidget->rect.x, selectedWidget->rect.y, selectedWidget->rect.w, selectedWidget->rect.h, app.mouse.x, app.mouse.y, 1, 1))
{
playSound(SND_GUI_SELECT);
selectedWidget->action();
}
}
}
static void loadWidgets(char *filename) static void loadWidgets(char *filename)
{ {
cJSON *root, *node; cJSON *root, *node;

View File

@ -31,6 +31,7 @@ extern int getDistance(int x1, int y1, int x2, int y2);
extern int mod(int n, int x); extern int mod(int n, int x);
extern void blit(SDL_Texture *texture, int x, int y, int centered); extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern void playSound(int id); extern void playSound(int id);
extern App app; extern App app;