From 8e17066be154ca620a3ac67ef33a71f1d42c3c94 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 8 Apr 2012 16:51:50 +0100 Subject: [PATCH] [Add] Added some input structures and methods. [Fix/Clean] Makefiles are uh.. a bitch! --- Bin/Makefile | 14 +++++-- src/Actor/Makefile | 2 +- src/Global/Globals.cpp | 3 ++ src/Global/Globals.h | 4 ++ src/Global/Makefile | 20 ++++++++++ src/IO/Input.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ src/IO/Input.h | 43 +++++++++++++++++++++ src/IO/Makefile | 20 ++++++++++ src/Main/GLWindow.cpp | 3 ++ src/Main/GLWindow.h | 3 ++ src/Main/Game.cpp | 2 +- src/Main/LGLXWindow.cpp | 3 +- src/Main/LGLXWindow.h | 1 + src/Math/Makefile | 2 +- src/Sprite/Makefile | 2 +- src/System/Makefile | 2 +- src/Texture/Makefile | 2 +- 17 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 src/Global/Globals.cpp create mode 100644 src/Global/Globals.h create mode 100644 src/Global/Makefile create mode 100644 src/IO/Input.cpp create mode 100644 src/IO/Input.h create mode 100644 src/IO/Makefile diff --git a/Bin/Makefile b/Bin/Makefile index 840424b..0501d3c 100644 --- a/Bin/Makefile +++ b/Bin/Makefile @@ -14,19 +14,25 @@ all: $(MAKE) -C ../src/Math $(MAKE) -C ../src/System $(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 \ - ../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: @echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m" @sleep 1 $(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 \ ../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: $(MAKE) -C ../src/Main/ clean @@ -35,4 +41,6 @@ clean: $(MAKE) -C ../src/Math/ clean $(MAKE) -C ../src/System/ clean $(MAKE) -C ../src/Sprite/ clean + $(MAKE) -C ../src/IO/ clean + $(MAKE) -C ../src/Global/ clean rm -f LibD diff --git a/src/Actor/Makefile b/src/Actor/Makefile index 12cf429..d9f50be 100644 --- a/src/Actor/Makefile +++ b/src/Actor/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_image -objects = Player.o \ +objects = *.o \ .PHONY: default all clean diff --git a/src/Global/Globals.cpp b/src/Global/Globals.cpp new file mode 100644 index 0000000..1fec055 --- /dev/null +++ b/src/Global/Globals.cpp @@ -0,0 +1,3 @@ +#include "Globals.h" + +SDL_Event event; \ No newline at end of file diff --git a/src/Global/Globals.h b/src/Global/Globals.h new file mode 100644 index 0000000..116d223 --- /dev/null +++ b/src/Global/Globals.h @@ -0,0 +1,4 @@ +#pragma once +#include + +extern SDL_Event event; \ No newline at end of file diff --git a/src/Global/Makefile b/src/Global/Makefile new file mode 100644 index 0000000..d9f50be --- /dev/null +++ b/src/Global/Makefile @@ -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) diff --git a/src/IO/Input.cpp b/src/IO/Input.cpp new file mode 100644 index 0000000..a99b5d3 --- /dev/null +++ b/src/IO/Input.cpp @@ -0,0 +1,82 @@ +#include +#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); +} diff --git a/src/IO/Input.h b/src/IO/Input.h new file mode 100644 index 0000000..988dfee --- /dev/null +++ b/src/IO/Input.h @@ -0,0 +1,43 @@ +#pragma once +#include + +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); diff --git a/src/IO/Makefile b/src/IO/Makefile new file mode 100644 index 0000000..d9f50be --- /dev/null +++ b/src/IO/Makefile @@ -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) diff --git a/src/Main/GLWindow.cpp b/src/Main/GLWindow.cpp index c222bdb..db851fe 100644 --- a/src/Main/GLWindow.cpp +++ b/src/Main/GLWindow.cpp @@ -1,3 +1,4 @@ +#ifdef _WIN32_ #include #include #include @@ -232,3 +233,5 @@ float GLWindow::GetElapsedSeconds(void) { _lastTime = currentTime; return seconds; } + +#endif diff --git a/src/Main/GLWindow.h b/src/Main/GLWindow.h index f9d15f9..7414721 100644 --- a/src/Main/GLWindow.h +++ b/src/Main/GLWindow.h @@ -1,4 +1,5 @@ #pragma once +#ifdef _WIN32_ // Stop makefiles from complaining. #include #include @@ -39,3 +40,5 @@ private: HINSTANCE _hinstance; // Application instance. WNDCLASSEX _windowClass; }; + +#endif diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 83f698c..24fbe3e 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -28,7 +28,7 @@ bool Game::Init(void) { _testSprite = new Sprite(); _testSprite->SetTexture(testTexture); _testSprite->SetHandle(Vec2(800/2, 600/2)); - _testSprite->SetScale(Vec2(1.0f, 1.0f)); + _testSprite->SetScale(Vec2(5.0f, 5.0f)); // Return success. return true; diff --git a/src/Main/LGLXWindow.cpp b/src/Main/LGLXWindow.cpp index b5b5246..ea987fe 100644 --- a/src/Main/LGLXWindow.cpp +++ b/src/Main/LGLXWindow.cpp @@ -35,7 +35,8 @@ LGLXWindow::LGLXWindow(void) : _width(0), _height(0), _bpp(0), - _GL3Supported(false) {} + _GL3Supported(false) + {} LGLXWindow::~LGLXWindow(void) { diff --git a/src/Main/LGLXWindow.h b/src/Main/LGLXWindow.h index 4efdc1c..eec4f77 100644 --- a/src/Main/LGLXWindow.h +++ b/src/Main/LGLXWindow.h @@ -4,6 +4,7 @@ #include #include "../glx/glxext.h" +#include "../IO/Input.h" #include #include diff --git a/src/Math/Makefile b/src/Math/Makefile index b8f56c2..1a3c264 100644 --- a/src/Math/Makefile +++ b/src/Math/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_image -objects = Timer.o FPS.o Vec2.o \ +objects = FPS.o Timer.o Vec2.o \ .PHONY: default all clean diff --git a/src/Sprite/Makefile b/src/Sprite/Makefile index a7b8f59..d9f50be 100644 --- a/src/Sprite/Makefile +++ b/src/Sprite/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_image -objects = Sprite.o \ +objects = *.o \ .PHONY: default all clean diff --git a/src/System/Makefile b/src/System/Makefile index 0928bb6..d9f50be 100644 --- a/src/System/Makefile +++ b/src/System/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_image -objects = Debug.o \ +objects = *.o \ .PHONY: default all clean diff --git a/src/Texture/Makefile b/src/Texture/Makefile index 06131af..d9f50be 100644 --- a/src/Texture/Makefile +++ b/src/Texture/Makefile @@ -2,7 +2,7 @@ CC = g++ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_image -objects = Texture.o \ +objects = *.o \ .PHONY: default all clean