From d9796041ca5876f88280ccb54560653e65e8da8a Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 30 Apr 2016 21:08:39 -0400 Subject: added TextureIterator --- include/common.hpp | 168 +++++++++++++---------------------------------------- 1 file changed, 40 insertions(+), 128 deletions(-) (limited to 'include/common.hpp') diff --git a/include/common.hpp b/include/common.hpp index c30f861..3712f62 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -10,14 +10,9 @@ // holy moly #include -#include #include #include -#include -#include #include -#include -#include #include #include @@ -29,55 +24,30 @@ #include #include +#include + #ifdef __WIN32__ typedef unsigned int uint; #undef near #endif -/** - * Defines how many game ticks should occur in one second, affecting how often - * game logic is handled. - */ - -#define TICKS_PER_SEC 20 +// the number of ticks that should occur in one second +constexpr const unsigned int TICKS_PER_SEC = 20; -/** - * Defines how many milliseconds each game tick will take. - */ - -#define MSEC_PER_TICK (1000 / TICKS_PER_SEC) - -/** - * This flag lets the compuler know that we are testing for segfault locations. - * If this flag is enabled, the function C(x) will print 'x' to terminal - */ +// the number of milliseconds inbetween each tick +constexpr const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; +// segfault-debugging output //#define SEGFAULT -/** - * This flag lets the compiler know that we want to use shaders. - */ - -#define SHADERS - -template -N abso(N v) { - if (v < 0) { - return v * -1; - }else - return v; -} - -template -float averagef(A v) { - float avg = 0; - for(auto &a : v) { - avg += a; - } - avg /= v.size(); - return avg; -} +#ifdef SEGFAULT +#define C(x) std::cout << x << std::endl +#else +#define C(x) +#endif +// printf's a message to the console with file/line info +#define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__) extern GLuint colorIndex; // Texture.cpp? @@ -90,6 +60,8 @@ typedef struct { int y; } ivec2; +typedef ivec2 dim2; + struct _vec2 { float x; float y; @@ -115,8 +87,6 @@ typedef struct { float z; } vec3; -typedef ivec2 dim2; - /** * This structure contains two sets of coordinates for ray drawing. */ @@ -126,140 +96,82 @@ typedef struct { vec2 end; } Ray; -struct col { +struct _color { float red; float green; float blue; - col operator-=(float a) { + _color operator-=(float a) { red-=a; green-=a; blue-=a; return{red+a,green+a,blue+a}; } - col operator+=(float a) { + _color operator+=(float a) { return{red+a,green+a,blue+a}; } - col operator=(float a) { + _color operator=(float a) { return{red=a,green=a,blue=a}; } }; -typedef col Color; - -/** - * Define the game's name (displayed in the window title). - */ - -#define GAME_NAME "Independent Study v0.7 alpha - NOW WITH lights and snow and stuff" - -extern bool uiLoop; -extern std::mutex mtx; - -/** - * Define the length of a single HLINE. - * The game has a great amount of elements that need to be drawn or detected, and having each - * of them use specific hard-coded numbers would be painful to debug. As a solution, this - * definition was made. Every item being drawn to the screen and most object detection/physic - * handling is done based off of this number. Increasing it will give the game a zoomed-in - * feel, while decreasing it will do the opposite. - * - */ - -#define HLINES(n) (static_cast(game::HLINE * n)) - -/** - * A 'wrapper' for libc's srand(), as we hope to eventually have our own random number - * generator. - */ - -#define initRand(s) srand(s) - - - -/** - * A 'wrapper' for libc's rand(), as we hope to eventually have our own random number - * generator. - */ +typedef struct _color Color; -#define getRand() rand() +// gets the length of `n` HLINEs +template +inline T HLINES(const T &n) +{ + return (static_cast(game::HLINE) * n); +} -#define randGet rand +// random number generator initializer (TODO decide how to random gen. numbers) #define randInit srand -/** - * Included in common.h is a prototype for DEBUG_prints, which writes a formatted - * string to the console containing the callee's file and line number. This macro simplifies - * it to a simple printf call. - * - * DEBUG must be defined for this macro to function. - */ - -#define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__) - -#ifdef SEGFAULT -#define C(x) std::cout << x << std::endl -#else -#define C(x) -#endif - -/** - * Defines pi for calculations that need it. - */ - -#define PI 3.1415926535 +// gets random number +#define randGet rand +// defines pi for calculations that need it. +constexpr const float PI = 3.1415926535f; // references the variable in main.cpp, used for drawing with the player extern vec2 offset; -// counts the number of times logic() (see main.cpp) has been called, for animating sprites -extern unsigned int loops; - +// the shader program created in main.cpp extern GLuint shaderProgram; /** * Prints a formatted debug message to the console, along with the callee's file and line * number. */ - void DEBUG_prints(const char* file, int line, const char *s,...); /** * Sets color using glColor3ub(), but handles potential overflow. */ - void safeSetColor(int r,int g,int b); /** * Sets color using glColor4ub(), but handles potential overflow. */ - void safeSetColorA(int r,int g,int b,int a); -/** - * We've encountered many problems when attempting to create delays for triggering - * the logic function. As a result, we decided on using the timing libraries given - * by in the standard C++ library. This function simply returns the amount - * of milliseconds that have passed since the epoch. - */ - +// use our own millis function if we can, windows doesn't like at the moment... #ifdef __WIN32__ -#define millis() SDL_GetTicks() +#define millis() SDL_GetTicks() #else unsigned int millis(void); #endif // __WIN32__ +// reads the names of files in a directory into the given string vector int getdir(std::string dir, std::vector &files); + +// sorts a vector of strings alphabetically void strVectorSortAlpha(std::vector *v); +// reads the given file into a buffer and returns a pointer to the buffer const char *readFile(const char *path); -int strCreateFunc(const char *equ); - -template -size_t arrAmt(N (&)[s]) {return s;} - +// aborts the program, printing the given error void UserError(std::string reason); #endif // COMMON_H -- cgit v1.2.3 From 83af97c8b7bbe564a37e9d9c11a086d3b79f73d4 Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Sun, 1 May 2016 17:52:44 -0400 Subject: Fixed some stuff --- Makefile | 1 + include/common.hpp | 4 ++-- main.cpp | 49 ++++++++++++++++++++++++++----------------------- src/entities.cpp | 4 ++-- src/gametime.cpp | 11 +++++++---- 5 files changed, 38 insertions(+), 31 deletions(-) (limited to 'include/common.hpp') diff --git a/Makefile b/Makefile index eb38b52..d742643 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ $(EXEC): $(CXXOUTDIR)/$(CXXOBJ) main.cpp @echo " CXX/LD main" @$(CXX) $(CXXFLAGS) $(CXXINC) $(CXXWARN) -o $(EXEC) main.cpp out/*.o $(LIBS) @rm -rf xml/*.dat + @rm -rf storyXML/*.dat $(CXXOUTDIR)/%.o: $(CXXSRCDIR)/%.cpp @echo " CXX " $< diff --git a/include/common.hpp b/include/common.hpp index 3712f62..e607c12 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -32,10 +32,10 @@ typedef unsigned int uint; #endif // the number of ticks that should occur in one second -constexpr const unsigned int TICKS_PER_SEC = 20; +const unsigned int TICKS_PER_SEC = 20; // the number of milliseconds inbetween each tick -constexpr const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; +const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; // segfault-debugging output //#define SEGFAULT diff --git a/main.cpp b/main.cpp index 6de7c03..4db0579 100644 --- a/main.cpp +++ b/main.cpp @@ -132,7 +132,7 @@ int main(int argc, char *argv[]){ // 'basic' OpenGL setup SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetSwapInterval(0); // v-sync + SDL_GL_SetSwapInterval(1); // v-sync SDL_ShowCursor(SDL_DISABLE); // hide the mouse glViewport(0, 0, game::SCREEN_WIDTH, game::SCREEN_HEIGHT); glEnable(GL_BLEND); @@ -224,8 +224,12 @@ int main(int argc, char *argv[]){ // the main loop, in all of its gloriousness.. gameRunning = true; - while (gameRunning) + std::thread([&]{while (gameRunning) mainLoop(); + }).detach(); + + while(gameRunning) + render(); // free library resources Mix_HaltMusic(); @@ -262,33 +266,32 @@ void mainLoop(void){ game::time::mainLoopHandler(); - if (currentMenu) - goto MENU; - - // handle keypresses - currentWorld could change here - prev = currentWorld; - ui::handleEvents(); + if (currentMenu) { + return; + } else { + // handle keypresses - currentWorld could change here + prev = currentWorld; + ui::handleEvents(); - if(prev != currentWorld){ - currentWorld->bgmPlay(prev); - ui::dialogBoxExists = false; - } + if(prev != currentWorld){ + currentWorld->bgmPlay(prev); + ui::dialogBoxExists = false; + } - if (game::time::tickHasPassed()) - logic(); + if (game::time::tickHasPassed()) + logic(); - currentWorld->update(player, game::time::getDeltaTime()); - currentWorld->detect(player); + currentWorld->update(player, game::time::getDeltaTime()); + currentWorld->detect(player); - if (++debugDiv == 20) { - debugDiv=0; + if (++debugDiv == 20) { + debugDiv=0; - fps = 1000 / game::time::getDeltaTime(); - if (!(debugDiv % 10)) - debugY = player->loc.y; + fps = 1000 / game::time::getDeltaTime(); + if (!(debugDiv % 10)) + debugY = player->loc.y; + } } -MENU: - render(); } void render() { diff --git a/src/entities.cpp b/src/entities.cpp index c90650e..d5d9af2 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -37,7 +37,7 @@ const char *randomDialog[RAND_DIALOG_COUNT] = { "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", "Frig.", - "The sine of theta equals the opposite over the hypotenuese.", + "The sine of theta equals the opposite over the hdaypotenuese.", "Did you know the developers spelt brazier as brazzier.", "What's a bagel? I don't know because I'm mormon" }; @@ -263,7 +263,7 @@ Object::Object() { canMove = false; maxHealth = health = 1; - + inv = NULL; } diff --git a/src/gametime.cpp b/src/gametime.cpp index 598cd4f..1005d84 100644 --- a/src/gametime.cpp +++ b/src/gametime.cpp @@ -3,11 +3,13 @@ #include static unsigned int tickCount = 0; -static unsigned int deltaTime = 1; +static float deltaTime = 1; // millisecond timers static unsigned int currentTime = 0; -static unsigned int prevTime, prevPrevTime; +static unsigned int prevTime; + +static float accum = 0.0f; namespace game { namespace time { @@ -41,8 +43,9 @@ namespace game { } bool tickHasPassed(void) { - if (prevPrevTime + MSEC_PER_TICK <= currentTime) { - prevPrevTime = currentTime; + accum += deltaTime; + if (accum > MSEC_PER_TICK) { + accum = 0.0f; return true; } -- cgit v1.2.3