[Add] Added some input structures and methods.

[Fix/Clean] Makefiles are uh.. a bitch!
This commit is contained in:
Rtch90 2012-04-08 16:51:50 +01:00
parent 0bc96ae060
commit 8e17066be1
17 changed files with 198 additions and 10 deletions

View File

@ -14,19 +14,25 @@ all:
$(MAKE) -C ../src/Math $(MAKE) -C ../src/Math
$(MAKE) -C ../src/System $(MAKE) -C ../src/System
$(MAKE) -C ../src/Sprite $(MAKE) -C ../src/Sprite
$(MAKE) -C ../src/IO
$(MAKE) -C ../src/Global
$(CC) $(CFLAGS) -o LibD ../src/Main/main.cpp ../src/Main/*.o ../src/Texture/*.o \ $(CC) $(CFLAGS) -o LibD ../src/Main/main.cpp ../src/Main/*.o ../src/Texture/*.o \
../src/Actor/*.o ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o $(LDADD) ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o \
../src/IO/*.o ../src/Global/*.o $(LDADD)
static: static:
@echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m" @echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m"
@sleep 1 @sleep 1
$(MAKE) -C ../src/Main/ ../src/Main/*.o ../src/Texture/*.o \ $(MAKE) -C ../src/Main/ ../src/Main/*.o ../src/Texture/*.o \
../src/Actor/*.o ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o \
../src/IO/*.o ../src/Global/*.o
$(CC) $(CFLAGS) -o build/LibD-static ../src/Main/main.cpp ../src/Main/*.o \ $(CC) $(CFLAGS) -o build/LibD-static ../src/Main/main.cpp ../src/Main/*.o \
../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o \ ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o \
../src/System/*.o ../src/Sprite/*.o $(LDADDSTATIC) ../src/System/*.o ../src/Sprite/*.o ../src/IO/*.o
../src/Global/*.o \
$(LDADDSTATIC)
clean: clean:
$(MAKE) -C ../src/Main/ clean $(MAKE) -C ../src/Main/ clean
@ -35,4 +41,6 @@ clean:
$(MAKE) -C ../src/Math/ clean $(MAKE) -C ../src/Math/ clean
$(MAKE) -C ../src/System/ clean $(MAKE) -C ../src/System/ clean
$(MAKE) -C ../src/Sprite/ clean $(MAKE) -C ../src/Sprite/ clean
$(MAKE) -C ../src/IO/ clean
$(MAKE) -C ../src/Global/ clean
rm -f LibD rm -f LibD

View File

@ -2,7 +2,7 @@ CC = g++
CFLAGS = -ansi -Wall -g CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = Player.o \ objects = *.o \
.PHONY: default all clean .PHONY: default all clean

3
src/Global/Globals.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "Globals.h"
SDL_Event event;

4
src/Global/Globals.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <SDL/SDL.h>
extern SDL_Event event;

20
src/Global/Makefile Normal file
View File

@ -0,0 +1,20 @@
CC = g++
CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = *.o \
.PHONY: default all clean
default: all
%.cpp: %.h
%.o: %.cpp
$(CC) $(CFLAGS) -c -o $@ $<
all: $(objects)
clean:
rm -f $(objects)

82
src/IO/Input.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <string.h>
#include "Input.h"
static mouse_t mouse;
static keyboard_t keyboard;
bool _curr_key(int index) {
return(keyboard.keys[index] != 0);
}
bool _old_key(int index) {
return(keyboard.oldKeys[index] != 0);
}
bool _curr_mouse(int button) {
return((mouse.buttons * SDL_BUTTON(button)) != 0);
}
bool _old_mouse(int button) {
return((mouse.oldButtons & SDL_BUTTON(button)) != 0);
}
bool CreateInput(void) {
memset(&keyboard, 0, sizeof(keyboard_t));
memset(&mouse, 0, sizeof(mouse_t));
SDL_PumpEvents();
SDL_PumpEvents();
unsigned char* tempKeys = SDL_GetKeyState(&keyboard.keycount);
keyboard.keys = (unsigned char*)malloc(sizeof(char) * keyboard.keycount);
keyboard.oldKeys = (unsigned char*)malloc(sizeof(char) * keyboard.keycount);
memcpy(keyboard.keys, tempKeys, sizeof(char) * keyboard.keycount);
mouse.buttons = SDL_GetMouseState(&mouse.dx, &mouse.dy);
return true;
}
void UpdateInput(void) {
SDL_PumpEvents();
keyboard.lastChar = -1;
mouse.oldx = mouse.dx;
mouse.oldy = mouse.dy;
mouse.oldButtons = SDL_GetMouseState(&mouse.dx, &mouse.dy);
memcpy(keyboard.oldKeys, keyboard.keys, sizeof(char) * keyboard.keycount);
unsigned char *tmp = SDL_GetKeyState(&keyboard.keycount);
memcpy(keyboard.keys, tmp, sizeof(char) * keyboard.keycount);
keyboard.mods = SDL_GetModState();
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_KEYDOWN) {
keyboard.lastChar = event.key.keysym.sym;
}
}
}
char GetKey(void) {
if(keyboard.lastChar != -1)
return (char)keyboard.lastChar;
return 0;
}
unsigned int GetX(void) { return mouse.dx; }
unsigned int GetY(void) { return mouse.dy; }
unsigned int GetOldX(void) { return mouse.oldx; }
unsigned int GetOldY(void) { return mouse.oldy; }
unsigned int GetMods(void) { return keyboard.mods; }
bool KeyDown(int index) { return(_curr_key(index) && !_old_key(index)); }
bool KeyStillDown(int index) { return(_curr_key(index) && _old_key(index)); }
bool KeyUp(int index) { return(!_curr_key(index) && _old_key(index)); }
bool KeyStillUp(int index) { return(!_curr_key(index) && !_old_key(index)); }
bool MouseDown(int button) { return(_curr_mouse(button) && !_old_mouse(button)); }
bool MouseStillDown(int button) { return(_curr_mouse(button) && _old_mouse(button)); }
bool MouseUp(int button) { return(!_curr_mouse(button) && _old_mouse(button)); }
bool MouseStillUp(int button) { return(!_curr_mouse(button) && !_old_mouse(button)); }
void DestroyInput(void) {
free(keyboard.keys);
free(keyboard.oldKeys);
}

43
src/IO/Input.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <SDL/SDL.h>
typedef struct mouse_s {
int dx, dy;
int oldx, oldy;
unsigned int buttons;
unsigned int oldButtons;
} mouse_t;
typedef struct keyboard_s {
unsigned char *keys;
unsigned char *oldKeys;
int keycount;
int lastChar;
unsigned int mods;
} keyboard_t;
typedef struct input_s {
mouse_t mouse;
keyboard_t keyboard;
} input_t;
bool CreateInput(void);
void UpdateInput(void);
char GetKey(void);
unsigned int GetX(void);
unsigned int GetY(void);
unsigned int GetOldX(void);
unsigned int GetOldY(void);
unsigned int GetMods(void);
bool KeyDown(int index);
bool KeyStillDown(int index);
bool KeyUp(int index);
bool KeyStillUp(int index);
bool MouseDown(int button);
bool MouseStillDown(int button);
bool MouseUp(int button);
bool MouseStillUp(int button);
void DestroyInput(void);

20
src/IO/Makefile Normal file
View File

@ -0,0 +1,20 @@
CC = g++
CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = *.o \
.PHONY: default all clean
default: all
%.cpp: %.h
%.o: %.cpp
$(CC) $(CFLAGS) -c -o $@ $<
all: $(objects)
clean:
rm -f $(objects)

View File

@ -1,3 +1,4 @@
#ifdef _WIN32_
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
#include <Windows.h> #include <Windows.h>
@ -232,3 +233,5 @@ float GLWindow::GetElapsedSeconds(void) {
_lastTime = currentTime; _lastTime = currentTime;
return seconds; return seconds;
} }
#endif

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#ifdef _WIN32_ // Stop makefiles from complaining.
#include <windows.h> #include <windows.h>
#include <ctime> #include <ctime>
@ -39,3 +40,5 @@ private:
HINSTANCE _hinstance; // Application instance. HINSTANCE _hinstance; // Application instance.
WNDCLASSEX _windowClass; WNDCLASSEX _windowClass;
}; };
#endif

View File

@ -28,7 +28,7 @@ bool Game::Init(void) {
_testSprite = new Sprite(); _testSprite = new Sprite();
_testSprite->SetTexture(testTexture); _testSprite->SetTexture(testTexture);
_testSprite->SetHandle(Vec2(800/2, 600/2)); _testSprite->SetHandle(Vec2(800/2, 600/2));
_testSprite->SetScale(Vec2(1.0f, 1.0f)); _testSprite->SetScale(Vec2(5.0f, 5.0f));
// Return success. // Return success.
return true; return true;

View File

@ -35,7 +35,8 @@ LGLXWindow::LGLXWindow(void) :
_width(0), _width(0),
_height(0), _height(0),
_bpp(0), _bpp(0),
_GL3Supported(false) {} _GL3Supported(false)
{}
LGLXWindow::~LGLXWindow(void) { LGLXWindow::~LGLXWindow(void) {

View File

@ -4,6 +4,7 @@
#include <GL/glx.h> #include <GL/glx.h>
#include "../glx/glxext.h" #include "../glx/glxext.h"
#include "../IO/Input.h"
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>

View File

@ -2,7 +2,7 @@ CC = g++
CFLAGS = -ansi -Wall -g CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = Timer.o FPS.o Vec2.o \ objects = FPS.o Timer.o Vec2.o \
.PHONY: default all clean .PHONY: default all clean

View File

@ -2,7 +2,7 @@ CC = g++
CFLAGS = -ansi -Wall -g CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = Sprite.o \ objects = *.o \
.PHONY: default all clean .PHONY: default all clean

View File

@ -2,7 +2,7 @@ CC = g++
CFLAGS = -ansi -Wall -g CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = Debug.o \ objects = *.o \
.PHONY: default all clean .PHONY: default all clean

View File

@ -2,7 +2,7 @@ CC = g++
CFLAGS = -ansi -Wall -g CFLAGS = -ansi -Wall -g
LDADD = -lGL -lGLU -lSDL -lSDL_image LDADD = -lGL -lGLU -lSDL -lSDL_image
objects = Texture.o \ objects = *.o \
.PHONY: default all clean .PHONY: default all clean