diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2016-10-10 19:17:19 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2016-10-10 19:17:19 -0500 |
commit | b19265bfa91e55c564b75aadcabd212ac89cf349 (patch) | |
tree | fab3a34fc96d52fc634ca0d507fdbaf5d3546b8e | |
parent | 1f762f82f929cfd21222739a627a32e6199c34a9 (diff) |
the revival, entityx
-rw-r--r-- | Changelog | 14 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/common.hpp | 23 | ||||
-rw-r--r-- | include/entities.hpp | 26 | ||||
-rw-r--r-- | include/events.hpp | 31 | ||||
-rw-r--r-- | include/inventory.hpp | 11 | ||||
-rw-r--r-- | include/render.hpp | 79 | ||||
-rw-r--r-- | include/ui.hpp | 11 | ||||
-rw-r--r-- | include/window.hpp | 20 | ||||
-rw-r--r-- | main.cpp | 201 | ||||
-rw-r--r-- | src/common.cpp | 48 | ||||
-rw-r--r-- | src/entities.cpp | 204 | ||||
-rw-r--r-- | src/inventory.cpp | 135 | ||||
-rw-r--r-- | src/render.cpp | 66 | ||||
-rw-r--r-- | src/ui.cpp | 732 | ||||
-rw-r--r-- | src/ui_menu.cpp | 98 | ||||
-rw-r--r-- | src/window.cpp | 71 | ||||
-rw-r--r-- | src/world.cpp | 159 | ||||
-rw-r--r-- | xml/!town.xml | 4 |
19 files changed, 1037 insertions, 898 deletions
@@ -1079,3 +1079,17 @@ Late June (6/16/2016 - 6/27/2016) - added q to drop items, as objects - chests snag dropped objects, right click to reclaim - control setting is good, needs saving though + + + + +10/10/2016: +=========== + + THE REVIVAL + + - began to implement EntityX + - things will actually start to work + - created render.cpp, moved shader management to there + - this game will be kinda decent now??? + - input bugs, because we're awful @@ -5,7 +5,7 @@ CXX = g++ ifeq ($(TARGET_OS),linux) LIBS = -lpthread -lGL -lGLEW -lfreetype \ - -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2main + -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2main -lentityx endif ifeq ($(TARGET_OS),win32) LIBS = -lopengl32 -lglew32 -lmingw32 \ diff --git a/include/common.hpp b/include/common.hpp index 12a7a50..8d74cda 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -50,15 +50,6 @@ typedef unsigned int uint; #undef near #endif -// Enables special message printing in the case of segfault debugging (unnecessary?) -//#define SEGFAULT - -#ifdef SEGFAULT -#define C(x) std::cout << x << '\n' -#else -#define C(x) -#endif - /** * Prints a formatted string to the terminal with file and line number, for debugging */ @@ -68,7 +59,7 @@ typedef unsigned int uint; * Creates a coordinate of integers. */ typedef struct { - int x; /**< The x coordinate */ + int x; /**< The x coordinate */ int y; /**< The y coordinate */ } ivec2; @@ -215,18 +206,6 @@ constexpr const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; std::vector<std::string> StringTokenizer(const std::string& str, char delim); /** - * A function used to tell the program what shader, attributes, and uniforms - * we want to draw our rectangles to. - * - * @see drawRect - * @param the shader - * @param the 'texture uniform' - * @param the 'coord attribute' - * @param the 'texture coord attribute' - */ -void useShader(GLuint *sh, GLint *tu, GLint *ca, GLint *ta); - -/** * A function to draw a colored box for OpenGL. * To use it, the lower left hand and upper right hand coords are given. * diff --git a/include/entities.hpp b/include/entities.hpp index 1bb2924..5f85c26 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -134,7 +134,7 @@ public: belongsTo = false; following = nullptr; - flame = false; + flame = false; } Light(vec2 l, float r, Color c) @@ -159,7 +159,7 @@ public: { flame = true; } - + void createFromXML(XMLElement *e); }; @@ -303,7 +303,7 @@ public: virtual void createFromXML(XMLElement *e, World *w=nullptr) =0; virtual void saveToXML(void) =0; -/* Entity +/* Entity ** * Forces the given entity to follow this one. @@ -498,6 +498,26 @@ constexpr Merchant *Merchantp(Entity *e) { return (Merchant *)e; } +#include <entityx/entityx.h> +#include <events.hpp> + +class PlayerSystem : public entityx::System<PlayerSystem>, public entityx::Receiver<PlayerSystem> { +private: + Player **m_Player; + bool m_MoveLeft, m_MoveRight; + +public: + PlayerSystem(Player **p) + : m_Player(p), m_MoveLeft(false), m_MoveRight(false) {} + + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; + + void configure(entityx::EventManager &ev); + void receive(const KeyDownEvent &kde); + void receive(const KeyUpEvent &kue); +}; + + #endif // ENTITIES_H /** diff --git a/include/events.hpp b/include/events.hpp new file mode 100644 index 0000000..aa3ca07 --- /dev/null +++ b/include/events.hpp @@ -0,0 +1,31 @@ +#ifndef EVENTS_HPP_ +#define EVENTS_HPP_ + +/** + * A place for events to live, while gamedev slowly dies from rewriting. + */ + +#include <SDL2/SDL.h> + + struct MouseScrollEvent { + MouseScrollEvent(int sd) + : scrollDistance(sd) {} + + int scrollDistance; + }; + +struct KeyDownEvent { + KeyDownEvent(SDL_Keycode kc) + : keycode(kc) {} + + SDL_Keycode keycode; +}; + +struct KeyUpEvent { + KeyUpEvent(SDL_Keycode kc) + : keycode(kc) {} + + SDL_Keycode keycode; +}; + +#endif // EVENTS_HPP_ diff --git a/include/inventory.hpp b/include/inventory.hpp index ca402b3..9a42aa4 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -5,6 +5,7 @@ #include <string.h> #include <texture.hpp> +#include <events.hpp> #define DEBUG @@ -268,4 +269,14 @@ GLuint getItemTexture(std::string name); float getItemWidth(std::string name); float getItemHeight(std::string name); +#include <entityx/entityx.h> + +class InventorySystem : public entityx::System<InventorySystem>, public entityx::Receiver<InventorySystem> { +public: + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; + + void configure(entityx::EventManager &em); + void receive(const MouseScrollEvent &mse); +}; + #endif // INVENTORY_H diff --git a/include/render.hpp b/include/render.hpp new file mode 100644 index 0000000..0a2adcd --- /dev/null +++ b/include/render.hpp @@ -0,0 +1,79 @@ +#ifndef RENDER_HPP_ +#define RENDER_HPP_ + +#include <vector> + +#include <GL/glew.h> +#include <SDL2/SDL_opengl.h> + +#include <common.hpp> +#include <shader_utils.hpp> + +/** + * @class Shader + * @brief Handles a texture shader, allowing it's use in the program. + */ +class Shader { +public: + GLuint shader; + GLint coord; + GLint tex; + std::vector<GLint> uniform; + + void create(const char *vert, const char *frag) { + shader = create_program(vert, frag); + coord = get_attrib(shader, "coord2d"); + tex = get_attrib(shader, "tex_coord"); + } + + inline void addUniform(const char *name) { + uniform.push_back(get_uniform(shader, name)); + } + + inline void use(void) { + glUseProgram(shader); + } + + inline void unuse(void) { + glUseProgram(0); + } + + inline void enable(void) { + glEnableVertexAttribArray(coord); + glEnableVertexAttribArray(tex); + } + + inline void disable(void) { + glDisableVertexAttribArray(coord); + glDisableVertexAttribArray(tex); + } + + ~Shader(void) { + uniform.clear(); + } +}; + +typedef enum { + WU_texture = 0, + WU_ortho, + WU_tex_color, + WU_transform, + WU_ambient, + WU_light_impact, + WU_light, + WU_light_color, + WU_light_size +} WorldUniform; + +namespace Render { + extern Shader worldShader; + extern Shader textShader; + + void initShaders(void); + + void useShader(Shader *s); + + void drawRect(vec2 ll, vec2 ur, float z); +} + +#endif // RENDER_HPP_ diff --git a/include/ui.hpp b/include/ui.hpp index 56c484c..f276882 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -43,8 +43,18 @@ void setControl(unsigned int index, SDL_Keycode key); SDL_Keycode getControl(unsigned int index); +#include <entityx/entityx.h> + +class InputSystem : public entityx::System<InputSystem> { +public: + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; +}; + namespace ui { + extern bool fadeEnable; + extern int fadeIntensity; + // the pixel-coordinates of the mouse extern vec2 mouse; @@ -155,6 +165,7 @@ namespace ui { void toggleWhite(void); void toggleWhiteFast(void); void waitForCover(void); + void waitForUncover(void); } diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..b642835 --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,20 @@ +#ifndef WINDOW_HPP_ +#define WINDOW_HPP_ + +#include <entityx/entityx.h> + +#include <SDL2/SDL.h> + +class WindowSystem : public entityx::System<WindowSystem> { +private: + SDL_Window *window; + SDL_GLContext glContext; + +public: + WindowSystem(void); + ~WindowSystem(void); + + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; +}; + +#endif // WINDOW_HPP_ @@ -8,6 +8,11 @@ #include <brice.hpp> +#include <entityx/entityx.h> + +#include <window.hpp> +#include <render.hpp> + // local library includes #include <tinyxml2.h> using namespace tinyxml2; @@ -27,12 +32,6 @@ using namespace tinyxml2; ** Variables section ** --------------------------------------------------------------------------*/ -// the game's window title name -constexpr const char *GAME_NAME = "Independent Study v0.8 alpha - NOW WITH decent shaders"; - -// SDL's window object -SDL_Window *window = NULL; - // main loop runs based on this variable's value bool gameRunning = true; @@ -53,37 +52,6 @@ Menu *currentMenu; // the player object Player *player; -/** - * These are the source and index variables for our shader - * used to draw text and ui elements - */ - -GLuint textShader; -GLint textShader_attribute_coord; -GLint textShader_attribute_tex; -GLint textShader_uniform_texture; -GLint textShader_uniform_transform; -GLint textShader_uniform_color; - -/** - * These are the source and index variables for the world - * shader which is used to draw the world items and shade them - */ - -GLuint worldShader; -GLint worldShader_attribute_coord; -GLint worldShader_attribute_tex; -GLint worldShader_uniform_texture; -GLint worldShader_uniform_texture_normal; -GLint worldShader_uniform_transform; -GLint worldShader_uniform_ortho; -GLint worldShader_uniform_color; -GLint worldShader_uniform_ambient; -GLint worldShader_uniform_light; -GLint worldShader_uniform_light_color; -GLint worldShader_uniform_light_impact; -GLint worldShader_uniform_light_amt; - // the ambient light of the current world Color ambient; @@ -118,9 +86,38 @@ void mainLoop(void); ** MAIN ************************************************************************ ********************************************************************************/ +class Engine : public entityx::EntityX { +public: + explicit Engine(void) {} + + void init(void) { + game::config::read(); + systems.add<WindowSystem>(); + systems.add<InputSystem>(); + systems.add<InventorySystem>(); + systems.add<PlayerSystem>(&player); + + systems.configure(); + } + + void render(entityx::TimeDelta dt) { + systems.update<WindowSystem>(dt); + } + + void update(entityx::TimeDelta dt) { + systems.update<InputSystem>(dt); + systems.update<InventorySystem>(dt); + systems.update<PlayerSystem>(dt); + + currentWorld->update(player, dt); + currentWorld->detect(player); + } +}; + +Engine engine; + int main(int argc, char *argv[]) { - static SDL_GLContext mainGLContext = NULL; static bool worldReset = false, worldDontReallyRun = false; std::string worldActuallyUseThisXMLFile; @@ -138,43 +135,7 @@ int main(int argc, char *argv[]) } } - // attempt to initialize SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) != 0) - UserError(std::string("SDL was not able to initialize! Error: ") + SDL_GetError()); - atexit(SDL_Quit); - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - - // attempt to initialize SDL_image - if (!(IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) & (IMG_INIT_PNG | IMG_INIT_JPG))) - UserError(std::string("Could not init image libraries! Error: ") + IMG_GetError()); - atexit(IMG_Quit); - - // attempt to initialize SDL_mixer - if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) - UserError(std::string("SDL_mixer could not initialize! Error: ") + Mix_GetError()); - Mix_AllocateChannels(8); - atexit(Mix_Quit); - - // update values by reading the config file (config/settings.xml) - game::config::read(); - - // create the SDL window object - window = SDL_CreateWindow(GAME_NAME, - SDL_WINDOWPOS_UNDEFINED, // Spawn the window at random (undefined) x and y coordinates - SDL_WINDOWPOS_UNDEFINED, // - game::SCREEN_WIDTH, - game::SCREEN_HEIGHT, - SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | (game::FULLSCREEN ? SDL_WINDOW_FULLSCREEN : 0) - ); - - if (window == NULL) - UserError(std::string("The window failed to generate! SDL_Error: ") + SDL_GetError()); - - // create the OpenGL object that SDL provides - if ((mainGLContext = SDL_GL_CreateContext(window)) == NULL) - UserError(std::string("The OpenGL context failed to initialize! SDL_Error: ") + SDL_GetError()); + engine.init(); // initialize GLEW #ifndef __WIN32__ @@ -203,38 +164,8 @@ int main(int argc, char *argv[]) // initialize shaders std::cout << "Initializing shaders!\n"; - /** - * Creating the text shader and its attributes/uniforms - */ - textShader = create_program("shaders/new.vert", "shaders/new.frag"); - - textShader_attribute_coord = get_attrib(textShader, "coord2d"); - textShader_attribute_tex = get_attrib(textShader, "tex_coord"); - - textShader_uniform_texture = get_uniform(textShader, "sampler"); - textShader_uniform_transform = get_uniform(textShader, "ortho"); - textShader_uniform_color = get_uniform(textShader, "tex_color"); - - /** - * Creating the world's shader and its attributes/uniforms - */ - worldShader = create_program("shaders/world.vert", "shaders/world.frag"); - - worldShader_attribute_coord = get_attrib(worldShader, "coord2d"); - worldShader_attribute_tex = get_attrib(worldShader, "tex_coord"); - - worldShader_uniform_texture = get_uniform(worldShader, "texture"); - worldShader_uniform_texture_normal = get_uniform(worldShader, "normalTex"); - worldShader_uniform_transform = get_uniform(worldShader, "transform"); - worldShader_uniform_ortho = get_uniform(worldShader, "ortho"); - worldShader_uniform_color = get_uniform(worldShader, "tex_color"); - worldShader_uniform_ambient = get_uniform(worldShader, "ambientLight"); - worldShader_uniform_light = get_uniform(worldShader, "light"); - worldShader_uniform_light_color = get_uniform(worldShader, "lightColor"); - worldShader_uniform_light_impact = get_uniform(worldShader, "lightImpact"); - worldShader_uniform_light_amt = get_uniform(worldShader, "lightSize"); - - //glEnable(GL_MULTISAMPLE); + // create shaders + Render::initShaders(); // load up some fresh hot brice game::briceLoad(); @@ -246,7 +177,6 @@ int main(int argc, char *argv[]) // load mouse texture, and other inventory textures mouseTex = Texture::loadTexture("assets/mouse.png"); - // spawn the player player = new Player(); player->sspawn(0,100); @@ -353,6 +283,7 @@ int main(int argc, char *argv[]) }).detach(); while (gameRunning) { + engine.render(0); render(); ui::handleEvents(); } @@ -368,9 +299,6 @@ int main(int argc, char *argv[]) ui::destroyFonts(); Texture::freeTextures(); - SDL_GL_DeleteContext(mainGLContext); - SDL_DestroyWindow(window); - // close up the game stuff currentWorld->save(); delete arena; @@ -391,8 +319,7 @@ void mainLoop(void){ if (game::time::tickHasPassed()) logic(); - currentWorld->update(player, game::time::getDeltaTime()); - currentWorld->detect(player); + engine.update(game::time::getDeltaTime()); } } @@ -433,16 +360,16 @@ void render() { glEnable(GL_DEPTH_TEST); //glEnable(GL_CULL_FACE); - glUseProgram(textShader); - glUniformMatrix4fv(textShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(ortho)); - glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0); - glUseProgram(worldShader); - glUniformMatrix4fv(worldShader_uniform_ortho, 1, GL_FALSE, glm::value_ptr(ortho)); - glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f))); + Render::textShader.use(); + glUniformMatrix4fv(Render::textShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(ortho)); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); + Render::worldShader.use(); + glUniformMatrix4fv(Render::worldShader.uniform[WU_ortho], 1, GL_FALSE, glm::value_ptr(ortho)); + glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f))); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); - glUniform4f(worldShader_uniform_ambient, ambient.red, ambient.green, ambient.blue, 1.0); - glUniform1f(worldShader_uniform_light_impact, 1.0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::worldShader.uniform[WU_ambient], ambient.red, ambient.green, ambient.blue, 1.0); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 1.0); /*static GLfloat l[] = {460.0, 100.0, 0.0, 300.0}; static GLfloat lc[] = {1.0, 1.0, 1.0, 1.0}; @@ -497,7 +424,7 @@ void render() { GLfloat tpoint[es * 2 * 5]; GLfloat *tp = &tpoint[0]; - glUseProgram(textShader); + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, tracerText); if (ui::posFlag) { @@ -518,16 +445,14 @@ void render() { } } - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_coord); + Render::worldShader.enable(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &tpoint[0]); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &tpoint[3]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &tpoint[0]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &tpoint[3]); glDrawArrays(GL_LINES, 0, es * 2); - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_tex); - glUseProgram(0); + Render::worldShader.disable(); + Render::worldShader.unuse(); } @@ -536,13 +461,12 @@ void render() { if (currentMenu) ui::menu::draw(); - glUseProgram(textShader); + Render::textShader.use(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, mouseTex); - glUniform1i(textShader_uniform_texture, 0); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_tex); - glEnableVertexAttribArray(textShader_attribute_coord); + Render::textShader.enable(); glDisable(GL_DEPTH_TEST); @@ -566,14 +490,11 @@ void render() { 0.0f, 0.0f, //bottom left }; - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, mouseCoords); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, mouseTex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, mouseCoords); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, mouseTex); glDrawArrays(GL_TRIANGLES, 0, 6); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - SDL_GL_SwapWindow(window); + Render::textShader.disable(); } void logic(){ diff --git a/src/common.cpp b/src/common.cpp index bb1ad1e..26f5abc 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -34,50 +34,6 @@ std::vector<std::string> StringTokenizer(const std::string& str, char delim) return tokens; } -static GLuint *Gshader; -static GLint *tex_uni; -static GLint *coord_attrib; -static GLint *tex_attrib; - -void useShader(GLuint *sh, GLint *tu, GLint *ca, GLint *ta) -{ - Gshader = sh; - tex_uni = tu; - coord_attrib = ca; - tex_attrib = ta; -} - -void drawRect(vec2 ll, vec2 ur, float z) -{ - GLfloat verts[] = {ll.x, ll.y, z, - ur.x, ll.y, z, - ur.x, ur.y, z, - - ur.x, ur.y, z, - ll.x, ur.y, z, - ll.x, ll.y, z}; - - GLfloat tex[] = {0.0, 1.0, - 1.0, 1.0, - 1.0, 0.0, - - 1.0, 0.0, - 0.0, 0.0, - 0.0, 1.0}; - - glUniform1i(*tex_uni, 0); - - glEnableVertexAttribArray(*coord_attrib); - glEnableVertexAttribArray(*tex_attrib); - - glVertexAttribPointer(*coord_attrib, 3, GL_FLOAT, GL_FALSE, 0, verts); - glVertexAttribPointer(*tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, tex); - glDrawArrays(GL_TRIANGLES, 0, 6); - - glDisableVertexAttribArray(*tex_attrib); - glDisableVertexAttribArray(*coord_attrib); -} - void DEBUG_prints(const char* file, int line, const char *s,...) { va_list args; @@ -118,7 +74,7 @@ int getdir(std::string dir, std::vector<std::string> &files) #else HANDLE dirh; WIN32_FIND_DATA file_data; - + if ((dirh = FindFirstFile((dir + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) return -1; /* No files found */ @@ -132,7 +88,7 @@ int getdir(std::string dir, std::vector<std::string> &files) if (is_directory) continue; - + files.push_back(file_name); } while (FindNextFile(dirh, &file_data)); diff --git a/src/entities.cpp b/src/entities.cpp index b93d1f0..fbab59f 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -9,6 +9,8 @@ #include <gametime.hpp> #include <brice.hpp> +#include <render.hpp> + extern std::istream *names; extern Player *player; // main.cpp @@ -28,6 +30,159 @@ const unsigned int NPC_INV_SIZE = 3; static std::vector<std::string> randomDialog (readFileA("assets/dialog_en-us")); + +void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)en; + (void)ev; + (void)dt; + + if (!m_MoveLeft && !m_MoveRight) + (*m_Player)->vel.x = 0; +} + +void PlayerSystem::configure(entityx::EventManager &ev) +{ + ev.subscribe<KeyDownEvent>(*this); + ev.subscribe<KeyUpEvent>(*this); +} + +extern std::array<SDL_Keycode, 6> controlMap; +extern World *currentWorldToLeft; +extern World *currentWorldToRight; +extern bool gameRunning; +extern bool inBattle; + +void PlayerSystem::receive(const KeyUpEvent &kue) +{ + auto p = *m_Player; + auto kc = kue.keycode; + + if (kc == SDLK_ESCAPE) { + ui::menu::toggle(); + p->save(); + } else if (kc == controlMap[1]) { + m_MoveLeft = false; + } else if (kc == controlMap[2]) { + m_MoveRight = false; + } else if (kc == controlMap[3] || kc == controlMap[4]) { + player->speed = 1; + } else if (kc == controlMap[5]) { + if (p->inv->invHover) { + p->inv->invHover = false; + } else { + if (!p->inv->selected) + p->inv->invOpening ^= true; + else + p->inv->selected = false; + + p->inv->mouseSel = false; + } + + // disable action ui + ui::action::disable(); + } +} + +void PlayerSystem::receive(const KeyDownEvent &kde) +{ + auto p = *m_Player; + auto kc = kde.keycode; + + auto worldSwitch = [&](const WorldSwitchInfo& wsi){ + p->canMove = false; + ui::toggleBlackFast(); + ui::waitForCover(); + wsi.first->bgmPlay(currentWorld); + std::tie(currentWorld, p->loc) = wsi; + ui::toggleBlackFast(); + ui::waitForUncover(); + p->canMove = true; + }; + + if ((kc == SDLK_SPACE) && (game::canJump & p->ground)) { + p->loc.y += HLINES(2); + p->vel.y = .4; + p->ground = false; + } + + if (!ui::dialogBoxExists || ui::dialogPassive) { + if (kc == controlMap[0]) { + if (inBattle) { + std::thread([&](void){ + auto thing = dynamic_cast<Arena *>(currentWorld)->exitArena(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + } else if (!ui::fadeIntensity) { + std::thread([&](void){ + auto thing = currentWorld->goInsideStructure(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + } + } else if (kc == controlMap[1]) { + if (!ui::fadeEnable) { + p->vel.x = -PLAYER_SPEED_CONSTANT; + if (std::stoi(game::getValue("Slow")) == 1) + p->vel.x /= 2.0f; + p->left = m_MoveLeft = true; + p->right = m_MoveRight = false; + if (currentWorldToLeft) { + std::thread([&](void){ + auto thing = currentWorld->goWorldLeft(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + } + } + } else if (kc == controlMap[2]) { + if (!ui::fadeEnable) { + p->vel.x = PLAYER_SPEED_CONSTANT; + if (std::stoi(game::getValue("Slow")) == 1) + p->vel.x /= 2.0f; + p->right = m_MoveRight = true; + p->left = m_MoveLeft = false; + if (currentWorldToRight) { + std::thread([&](void){ + auto thing = currentWorld->goWorldRight(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + } + } + } else if (kc == controlMap[3]) { + if (game::canSprint) + p->speed = 2.0f; + } else if (kc == controlMap[4]) { + p->speed = .5; + } else if (kc == controlMap[5]) { + static int heyOhLetsGo = 0; + + //edown = true; + + // start hover counter? + if (!heyOhLetsGo) { + heyOhLetsGo = game::time::getTickCount(); + p->inv->mouseSel = false; + } + + // run hover thing + if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(p->inv->invOpen) && !(p->inv->selected)) { + p->inv->invHover = true; + + // enable action ui + ui::action::enable(); + } + } + } else if (kc == SDLK_DELETE) { + gameRunning = false; + } else if (kc == SDLK_t) { + game::time::tick(50); + } +} + + void getRandomName(Entity *e) { auto names = readFileA("assets/names_en-us"); @@ -417,21 +572,17 @@ void NPC::drawThingy(void) const // TODO use texture made for this static GLuint thingyColor = Texture::genColor(Color(236, 238, 15)); - glUseProgram(worldShader); - - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.use(); + Render::worldShader.enable(); glBindTexture(GL_TEXTURE_2D, thingyColor); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coords); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); glDrawArrays(GL_TRIANGLES, 0, 6); - glDisableVertexAttribArray(worldShader_attribute_coord); - glDisableVertexAttribArray(worldShader_attribute_tex); - - glUseProgram(0); + Render::worldShader.disable(); + Render::worldShader.unuse(); } } @@ -514,25 +665,24 @@ void Entity::draw(void) break; } - glUseProgram(worldShader); + Render::worldShader.use(); // make the entity hit flash red if (maxHitDuration-hitDuration) { float flashAmt = 1-(hitDuration/maxHitDuration); - glUniform4f(worldShader_uniform_color, 1.0, flashAmt, flashAmt, 1.0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, flashAmt, flashAmt, 1.0); } - glUniform1i(worldShader_uniform_texture, 0); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + Render::worldShader.enable(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); if (left) - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL); else - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord); glDrawArrays(GL_TRIANGLES, 0, 6); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); NOPE: if (near && type != MOBT) ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-game::HLINE/2,name); @@ -540,7 +690,7 @@ if (health != maxHealth) { static GLuint frontH = Texture::genColor(Color(255,0,0)); static GLuint backH = Texture::genColor(Color(150,0,0)); - glUniform1i(worldShader_uniform_texture, 0); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); GLfloat coord_back[] = { loc.x, loc.y + height, z + 0.1f, @@ -571,20 +721,18 @@ if (health != maxHealth) { 0.0, 1.0, 0.0, 0.0, }; - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coord_back); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord_back); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); glDrawArrays(GL_TRIANGLES, 0, 6); glBindTexture(GL_TEXTURE_2D, frontH); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coord_front); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord_front); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); glDrawArrays(GL_TRIANGLES, 0, 6); } -glDisableVertexAttribArray(worldShader_attribute_coord); -glDisableVertexAttribArray(worldShader_attribute_tex); - -glUseProgram(0); +Render::worldShader.disable(); +Render::worldShader.unuse(); } /** diff --git a/src/inventory.cpp b/src/inventory.cpp index 48adb86..f7a8df7 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -3,9 +3,10 @@ #include <numeric> #include <entities.hpp> -#include <ui.hpp> #include <gametime.hpp> +#include <render.hpp> + #include <tinyxml2.h> using namespace tinyxml2; @@ -93,7 +94,7 @@ void items(void) // light } else if (strCaseCmp(name, "light")) { - + ItemMap.push_back(new ItemLight()); // if type was not specified make it a base item @@ -312,7 +313,6 @@ void Inventory::draw(void) { float angle = float(angleB/2.0f); unsigned int a = 0; //static vec2 mouseStart = {0,0}; - C("End define"); auto deltaTime = game::time::getDeltaTime(); auto SCREEN_WIDTH = game::SCREEN_WIDTH; @@ -343,7 +343,7 @@ void Inventory::draw(void) { }; ui::fontTransInv = std::clamp(255 * (averagef(dfp) / range), 0ul, 255ul); - + if (invOpening) { for (auto &d : dfp) { if (!a || dfp[a - 1] > 50) @@ -403,38 +403,35 @@ void Inventory::draw(void) { * a = 0 */ - C("Start drawing inventory"); if (invOpen) { - useShader(&textShader, - &textShader_uniform_texture, - &textShader_attribute_coord, - &textShader_attribute_tex); + Render::useShader(&Render::textShader); for(auto &mr : massRay) { float t = (((float)massDfp[a]/(float)massRange)*.5f); glActiveTexture(GL_TEXTURE0); - glUseProgram(textShader); + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(0.0f,0.0f,0.0f, t >= 0? 255*t : 0))); - glUniform1i(textShader_uniform_texture, 0); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - drawRect(vec2(mr.x-(itemWide/2), mr.y-(itemWide/2)), vec2(mr.x-(itemWide/2)+itemWide, mr.y-(itemWide/2)+itemWide), -6.0); + Render::drawRect(vec2(mr.x-(itemWide/2), mr.y-(itemWide/2)), vec2(mr.x-(itemWide/2)+itemWide, mr.y-(itemWide/2)+itemWide), -6.0); glUseProgram(0); if (!Items.empty() && a+numSlot < Items.size() && Items[a+numSlot].second) { - glUseProgram(textShader); + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, Items[a+numSlot].first->tex->image[0]);//itemtex[items[a+numSlot].id]); - glUniform4f(textShader_uniform_color, 1.0f, 1.0f, 1.0f, ((float)massDfp[a]/(float)(massRange?massRange:1))*0.8f); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, ((float)massDfp[a]/(float)(massRange?massRange:1))*0.8f); + if (Items[a+numSlot].first->dim.y > Items[a+numSlot].first->dim.x) { - drawRect(vec2(mr.x-((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)), mr.y-(itemWide/2)), - vec2(mr.x+((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)), mr.y+(itemWide/2)), -6.1); - }else{ - drawRect(vec2(mr.x-(itemWide/2),mr.y-(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x)), - vec2(mr.x-(itemWide/2),mr.y+(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x)), -6.1); + Render::drawRect(vec2(mr.x-((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)), mr.y-(itemWide/2)), + vec2(mr.x+((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)), mr.y+(itemWide/2)), -6.1); + } else { + Render::drawRect(vec2(mr.x-(itemWide/2),mr.y-(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x)), + vec2(mr.x-(itemWide/2),mr.y+(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x)), -6.1); } ui::setFontColor(255,255,255,((float)massDfp[a]/(float)(massRange?massRange:1))*255); ui::putText(mr.x-(itemWide/2)+(itemWide*.85),mr.y-(itemWide/2),"%d",Items[a+numSlot].second); ui::setFontColor(255,255,255,255); - glUseProgram(0); + Render::textShader.unuse(); } a++; }a=0; @@ -446,11 +443,11 @@ void Inventory::draw(void) { float curTrans = (((float)curdfp[a]/(float)(curRange?curRange:1))*0.5f); - glUseProgram(textShader); + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(0.0f, 0.0f, 0.0f, curTrans >= 0 ? 255 * curTrans : 0))); - drawRect(vec2(cr.end.x-(itemWide/2), cr.end.y-(itemWide/2)), - vec2(cr.end.x-(itemWide/2)+itemWide,cr.end.y-(itemWide/2)+itemWide), -6.0); - glUseProgram(0); + Render::drawRect(vec2(cr.end.x-(itemWide/2), cr.end.y-(itemWide/2)), + vec2(cr.end.x-(itemWide/2)+itemWide,cr.end.y-(itemWide/2)+itemWide), -6.0); + Render::textShader.unuse(); a++; }a=0; @@ -462,27 +459,27 @@ void Inventory::draw(void) { float t = ((float)dfp[a]/(float)(range?range:1))*0.5f; - glUseProgram(textShader); + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(0.0f, 0.0f, 0.0f, t >= 0 ? 255 * t : 0))); - drawRect(vec2(r.end.x-(itemWide/2), r.end.y-(itemWide/2)), - vec2(r.end.x-(itemWide/2)+itemWide,r.end.y-(itemWide/2)+itemWide), -6.0); + Render::drawRect(vec2(r.end.x-(itemWide/2), r.end.y-(itemWide/2)), + vec2(r.end.x-(itemWide/2)+itemWide,r.end.y-(itemWide/2)+itemWide), -6.0); if (!Items.empty() && a < numSlot && Items[a].second) { glBindTexture(GL_TEXTURE_2D, Items[a].first->tex->image[0]);//itemtex[items[a].id]); - glUniform4f(textShader_uniform_color, 1.0f, 1.0f, 1.0f, ((float)dfp[a]/(float)(range?range:1))*0.8f); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, ((float)dfp[a]/(float)(range?range:1))*0.8f); if (Items[a].first->dim.y > Items[a].first->dim.x) { - drawRect(vec2(r.end.x-((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)), r.end.y-(itemWide/2)), - vec2(r.end.x+((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)), r.end.y+(itemWide/2)), -6.1); - }else{ - drawRect(vec2(r.end.x-(itemWide/2),r.end.y-(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x)), - vec2(r.end.x+(itemWide/2),r.end.y+(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x)), -6.1); + Render::drawRect(vec2(r.end.x-((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)), r.end.y-(itemWide/2)), + vec2(r.end.x+((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)), r.end.y+(itemWide/2)), -6.1); + } else { + Render::drawRect(vec2(r.end.x-(itemWide/2),r.end.y-(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x)), + vec2(r.end.x+(itemWide/2),r.end.y+(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x)), -6.1); } ui::setFontColor(255,255,255,((float)dfp[a]/(float)(range?range:1))*255); ui::putStringCentered(r.end.x,r.end.y-(itemWide*.9),Items[a].first->name);//itemMap[items[a].id]->name); ui::putText(r.end.x-(itemWide/2)+(itemWide*.85),r.end.y-(itemWide/2),"%d",Items[a].second); ui::setFontColor(255,255,255,255); } - glUseProgram(0); + Render::textShader.unuse(); if (sel == a) { static float sc = 1; static bool up; @@ -496,39 +493,35 @@ void Inventory::draw(void) { sc = 1.0; } float t = ((float)dfp[a]/(float)(range?range:1)); - useShader(&textShader, - &textShader_uniform_texture, - &textShader_attribute_coord, - &textShader_attribute_tex); + Render::useShader(&Render::textShader); - glUseProgram(textShader); - glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + Render::textShader.use(); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); // bottom glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t >= 0 ? 255 * t : 0))); - drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), - vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2), -6.2); + Render::drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), + vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2), -6.2); // top glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t >= 0 ? 255 * t : 0))); - drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), - vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2), -6.2); + Render::drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), + vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2), -6.2); // left glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t >= 0 ? 255 * t : 0))); - drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), - vec2(r.end.x - (itemWide*sc)/2 ,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), -6.2); + Render::drawRect(vec2(r.end.x - (itemWide*sc)/2 - (itemWide*sc)*.09,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), + vec2(r.end.x - (itemWide*sc)/2 ,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), -6.2); // right glBindTexture(GL_TEXTURE_2D, Texture::genColor(Color(255, 255, 255, t >= 0 ? 255 * t : 0))); - drawRect(vec2(r.end.x + (itemWide*sc)/2 ,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), - vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), -6.2); + Render::drawRect(vec2(r.end.x + (itemWide*sc)/2 ,r.end.y - (itemWide*sc)/2 - (itemWide*sc)*.09), + vec2(r.end.x + (itemWide*sc)/2 + (itemWide*sc)*.09,r.end.y + (itemWide*sc)/2 + (itemWide*sc)*.09), -6.2); //glUseProgram(0); } a++; } - C("Done drawing standard inv"); } /*else if (invHover) { static unsigned int highlight = 0; static unsigned int thing = 0; @@ -622,7 +615,7 @@ void itemDraw(Player *p, Item *d) { itemLoc.y = p->loc.y+(p->height/3); itemLoc.x = p->left?p->loc.x-d->dim.x/2:p->loc.x+p->width-d->dim.x/2; - glUseProgram(worldShader); + Render::worldShader.use(); if (p->left) { // move to center of screen @@ -636,7 +629,7 @@ void itemDraw(Player *p, Item *d) { glm::mat4 trt = glm::translate(glm::mat4(1.0f), glm::vec3(-itemLoc.x-d->dim.x/2, -itemLoc.y, 0)); // tell shader to translate the object using steps above - glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(tro * rot * trt)); + glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(tro * rot * trt)); } else { // move to center of screen glm::mat4 tro = glm::translate(glm::mat4(1.0f), @@ -649,7 +642,7 @@ void itemDraw(Player *p, Item *d) { glm::mat4 trt = glm::translate(glm::mat4(1.0f), glm::vec3(-itemLoc.x-d->dim.x/2,-itemLoc.y,0)); // tell shader to translate the object using steps above - glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(tro * rot * trt)); + glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(tro * rot * trt)); } GLfloat itemTex[12] = {0.0, 0.0, @@ -678,17 +671,14 @@ void itemDraw(Player *p, Item *d) { glBindTexture(GL_TEXTURE_2D,d->tex->image[0]); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, itemCoords); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, itemTex); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, itemCoords); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, itemTex); glDrawArrays(GL_TRIANGLES, 0, 6); - glDisableVertexAttribArray(worldShader_attribute_coord); - glDisableVertexAttribArray(worldShader_attribute_tex); - - glUseProgram(0); + Render::worldShader.disable(); + Render::worldShader.unuse(); } /* @@ -731,3 +721,28 @@ const Item* Inventory::getCurrentItem(void) else return nullptr; } + +void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)en; + (void)ev; + (void)dt; +} + +void InventorySystem::configure(entityx::EventManager &em) +{ + em.subscribe<MouseScrollEvent>(*this); +} + +void InventorySystem::receive(const MouseScrollEvent &mse) +{ + static int mouseWheelUpCount = 0, mouseWheelDownCount = 0; + + if ((mse.scrollDistance < 0) && ((++mouseWheelUpCount % 5) == 0) ) { + mouseWheelUpCount = 0; + player->inv->setSelectionUp(); + } else if ( (++mouseWheelDownCount % 5) == 0 ) { + mouseWheelDownCount = 0; + player->inv->setSelectionDown(); + } +} diff --git a/src/render.cpp b/src/render.cpp new file mode 100644 index 0000000..0c522f6 --- /dev/null +++ b/src/render.cpp @@ -0,0 +1,66 @@ +#include <render.hpp> + +static Shader *currentShader = nullptr; + +namespace Render { + +Shader worldShader; +Shader textShader; + +void initShaders(void) +{ + // create the world shader + worldShader.create("shaders/world.vert", "shaders/world.frag"); + worldShader.addUniform("texture"); + worldShader.addUniform("ortho"); + worldShader.addUniform("tex_color"); + worldShader.addUniform("transform"); + worldShader.addUniform("ambientLight"); + worldShader.addUniform("lightImpact"); + worldShader.addUniform("light"); + worldShader.addUniform("lightColor"); + worldShader.addUniform("lightSize"); + + // create the text shader + textShader.create("shaders/new.vert", "shaders/new.frag"); + textShader.addUniform("sampler"); + textShader.addUniform("ortho"); // actually not used, ortho in new.vert is mislabeled (actually transform) + textShader.addUniform("tex_color"); + textShader.addUniform("ortho"); // this is transform +} + +void useShader(Shader *s) +{ + currentShader = s; +} + +void drawRect(vec2 ll, vec2 ur, float z) +{ + GLfloat verts[] = {ll.x, ll.y, z, + ur.x, ll.y, z, + ur.x, ur.y, z, + + ur.x, ur.y, z, + ll.x, ur.y, z, + ll.x, ll.y, z}; + + GLfloat tex[] = {0.0, 1.0, + 1.0, 1.0, + 1.0, 0.0, + + 1.0, 0.0, + 0.0, 0.0, + 0.0, 1.0}; + + glUniform1i(currentShader->uniform[WU_texture], 0); + currentShader->enable(); + + glVertexAttribPointer(currentShader->coord, 3, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(currentShader->tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glDrawArrays(GL_TRIANGLES, 0, 6); + + currentShader->disable(); +} + + +} @@ -5,6 +5,9 @@ #include <world.hpp> #include <gametime.hpp> +#include <render.hpp> +#include <events.hpp> + extern Menu* currentMenu; extern SDL_Window *window; @@ -30,7 +33,7 @@ extern std::vector<NPC *> aipreload; extern bool gameRunning; -static std::array<SDL_Keycode, 6> controlMap = { +std::array<SDL_Keycode, 6> controlMap = { SDLK_w, SDLK_a, SDLK_d, SDLK_LSHIFT, SDLK_LCTRL, SDLK_e }; @@ -93,10 +96,8 @@ extern void mainLoop(void); * Fade effect flags */ -static bool fadeEnable = false; static bool fadeWhite = false; static bool fadeFast = false; -static int fadeIntensity = 0; bool inBattle = false; Mix_Chunk *battleStart; @@ -163,6 +164,9 @@ void loadFontSize(unsigned int size, std::vector<GLuint> &tex, std::vector<FT_In namespace ui { + bool fadeEnable = false; + int fadeIntensity = 0; + /* * Mouse coordinates. */ @@ -324,14 +328,12 @@ namespace ui { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,(*ftex)[c-33]); - glUniform1i(textShader_uniform_texture, 0); + glUniform1i(Render::textShader.uniform[WU_texture], 0); //glDisable(GL_DEPTH_TEST); - glUseProgram(textShader); - - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); GLfloat tex_coord[] = { 0.0, 1.0, //bottom left @@ -354,22 +356,20 @@ namespace ui { c1.x, c1.y -c2.y, fontZ //bottom left }; - glUniform4f(textShader_uniform_color, + glUniform4f(Render::textShader.uniform[WU_tex_color], static_cast<float>(fontColor[0]/255), static_cast<float>(fontColor[1]/255), static_cast<float>(fontColor[2]/255), static_cast<float>(fontColor[3]/255)); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, text_vert); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, text_vert); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); glDrawArrays(GL_TRIANGLES, 0, 6); - glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); - glDisableVertexAttribArray(textShader_attribute_tex); - glDisableVertexAttribArray(textShader_attribute_coord); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); //glEnable(GL_DEPTH_TEST); @@ -538,7 +538,7 @@ namespace ui { va_list args; std::unique_ptr<char[]> buf (new char[512]); memset(buf.get(), 0, 512 * sizeof(char)); - + va_start(args, str); vsnprintf(buf.get(), 512, str, args); va_end(args); @@ -647,7 +647,7 @@ namespace ui { while (fadeIntensity < 255); fadeIntensity = 255; } - + void waitForUncover(void) { while (fadeIntensity > 0); fadeIntensity = 0; @@ -726,27 +726,24 @@ namespace ui { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, boxT); - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, box); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, box); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex); glDrawArrays(GL_TRIANGLES, 0 ,6); glBindTexture(GL_TEXTURE_2D, lineT); - glUniform1i(textShader_uniform_texture, 0); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, line_strip); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, line_strip); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, box_tex); glDrawArrays(GL_LINE_STRIP, 0 ,8); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); } void drawNiceBox(vec2 c1, vec2 c2, float z) { @@ -758,15 +755,15 @@ namespace ui { // the dimensions of the corner textures static dim2 box_corner_dim_t = Texture::imageDim("assets/ui/button_corners.png"); static vec2 box_corner_dim = vec2(box_corner_dim_t.x / 2.0, box_corner_dim_t.y / 2.0); - + // the amount of bytes to skip in the OpenGL arrays (see below) auto stride = 5 * sizeof(GLfloat); - + // we always want to make sure c1 is lower left and c2 is upper right if (c1.x > c2.x) std::swap(c1.x, c2.y); if (c1.y > c2.y) std::swap(c1.y, c2.y); - - // if the box is too small, we will not be able to draw it + + // if the box is too small, we will not be able to draw it if (c2.x - c1.x < (box_corner_dim_t.x)) return; if (c2.y - c1.y < (box_corner_dim_t.y)) return; @@ -845,67 +842,64 @@ namespace ui { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, box_corner); - glUniform1f(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1f(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); // draw upper left corner - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ul[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ul[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ul[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ul[3]); glDrawArrays(GL_TRIANGLES, 0, 6); // lower left corner - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ll[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ll[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ll[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ll[3]); glDrawArrays(GL_TRIANGLES, 0, 6); - + // upper right corner - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ur[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ur[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_ur[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_ur[3]); glDrawArrays(GL_TRIANGLES, 0, 6); - + // lower right corner - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_lr[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_lr[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_lr[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_lr[3]); glDrawArrays(GL_TRIANGLES, 0, 6); // draw the middle of the box - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_f[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_f[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_f[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_f[3]); glDrawArrays(GL_TRIANGLES, 0, 6); - + glBindTexture(GL_TEXTURE_2D, box_side); // draw the left edge of the box - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_l[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_l[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_l[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_l[3]); glDrawArrays(GL_TRIANGLES, 0, 6); // draw right edge of the box - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_r[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_r[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_r[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_r[3]); glDrawArrays(GL_TRIANGLES, 0, 6); - + glBindTexture(GL_TEXTURE_2D, box_side_top); // draw bottom of the box - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_b[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_b[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_b[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_b[3]); glDrawArrays(GL_TRIANGLES, 0, 6); // draw top of the box - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, stride, &box_t[0]); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, stride, &box_t[3]); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, stride, &box_t[0]); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, stride, &box_t[3]); glDrawArrays(GL_TRIANGLES, 0, 6); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); } - + void draw(void){ unsigned char i; float x,y,tmp; @@ -940,20 +934,17 @@ namespace ui { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, pageTex); - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, page_loc); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, page_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, page_loc); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, page_tex); glDrawArrays(GL_TRIANGLES, 0 ,6); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); } else if (dialogBoxExists) { rtext = typeOut(dialogBoxText); @@ -1020,26 +1011,23 @@ namespace ui { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[1])); - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, left_item); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, left_item); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex); glDrawArrays(GL_TRIANGLES, 0 ,6); glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[0])); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, right_item); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, right_item); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex); glDrawArrays(GL_TRIANGLES, 0 ,6); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); merchArrowLoc[0].x = offset.x - (SCREEN_WIDTH / 8.5) - 16; merchArrowLoc[1].x = offset.x + (SCREEN_WIDTH / 8.5) + 16; @@ -1068,20 +1056,17 @@ namespace ui { merchArrowLoc[i].z, merchArrowLoc[i].y - 8, -7.1, merchArrowLoc[i].z, merchArrowLoc[i].y + 8, -7.1}; - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, tri_c); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tri_t); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, tri_c); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tri_t); glDrawArrays(GL_TRIANGLES, 0 ,6); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); } @@ -1175,28 +1160,25 @@ namespace ui { hub.x + 150, hub.y + 12, -7.1}; - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); glBindTexture(GL_TEXTURE_2D, frontHealth); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, front); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, front); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindTexture(GL_TEXTURE_2D, backHealth); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, back); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, back); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - - glUseProgram(0); + Render::textShader.disable(); + Render::textShader.unuse(); } /* @@ -1300,331 +1282,6 @@ EXIT: } void handleEvents(void) { - static bool left=true,right=false; - static int heyOhLetsGo = 0; - static int mouseWheelUpCount = 0, mouseWheelDownCount = 0; - - auto SCREEN_WIDTH = game::SCREEN_WIDTH; - auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; - - auto indoor = dynamic_cast<IndoorWorld *>(currentWorld); - Mob *m; // ;lkjfdsa - Entity *en; // used for interaction - - SDL_Event e; - - // update mouse coords - mouse.x = premouse.x + offset.x - (SCREEN_WIDTH / 2); - mouse.y = (offset.y + SCREEN_HEIGHT / 2) - premouse.y; - - static vec2 fr; - static Entity *ig; - - auto worldSwitch = [&](const WorldSwitchInfo& wsi){ - player->canMove = false; - toggleBlackFast(); - waitForCover(); - wsi.first->bgmPlay(currentWorld); - std::tie(currentWorld, player->loc) = wsi; - toggleBlackFast(); - waitForUncover(); - player->canMove = true; - }; - - while(SDL_PollEvent(&e)) { - switch(e.type) { - - // escape - quit game - case SDL_QUIT: - gameRunning = false; - break; - - // mouse movement - update mouse vector - case SDL_MOUSEMOTION: - premouse.x=e.motion.x; - premouse.y=e.motion.y; - break; - - case SDL_MOUSEBUTTONUP: - if (ig) { - ig->vel.x = (fr.x - mouse.x) / 50.0f; - ig->vel.y = (fr.y - mouse.y) / 50.0f; - //ig->forcedMove = true; // kills vel.x too quickly - ig = NULL; - } - break; - - // mouse clicks - case SDL_MOUSEBUTTONDOWN: - - // run actions? - if ((action::make = e.button.button & SDL_BUTTON_RIGHT)) - /*player->inv->invHover =*/ edown = false; - - textToDraw.clear(); - - if (dialogBoxExists || pageTexReady) { - // right click advances dialog - if ((e.button.button & SDL_BUTTON_RIGHT)) - dialogAdvance(); - } else { - // left click uses item - if (e.button.button & SDL_BUTTON_LEFT) { - if ((en = currentWorld->getNearMob(*player)) != nullptr) { - player->inv->currentAddInteract(en); - } - player->inv->useCurrent(); - } - - } - - if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width && - mouse.y > player->loc.y && mouse.y < player->loc.y + player->height) { - player->vel.y = .05; - fr = mouse; - ig = player; - } else { - for (auto &e : currentWorld->entity) { - if (mouse.x > e->loc.x && mouse.x < e->loc.x + e->width && - mouse.y > e->loc.y && mouse.y < e->loc.y + e->height) { - e->vel.y = .05; - fr = mouse; - ig = e; - break; - } - } - } - - break; - case SDL_MOUSEWHEEL: - if (e.wheel.y < 0) { - if (mouseWheelUpCount++ && mouseWheelUpCount%5==0) { - player->inv->setSelectionUp(); - mouseWheelUpCount = 0; - } - }else{ - if (mouseWheelDownCount-- && mouseWheelDownCount%5==0) { - player->inv->setSelectionDown(); - mouseWheelDownCount = 0; - } - } - break; - // key presses - case SDL_KEYDOWN: - - // space - make player jump - if (SDL_KEY == SDLK_SPACE && game::canJump) { - if (player->ground) { - player->loc.y += HLINES(2); - player->vel.y = .4; - player->ground = false; - } - break; - - // only let other keys be handled if dialog allows it - } else if (!dialogBoxExists || dialogPassive) { - if (SDL_KEY == controlMap[0]) { - if (inBattle) { - std::thread([&](void){ - auto thing = dynamic_cast<Arena *>(currentWorld)->exitArena(player); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - } else if (!fadeIntensity) { - std::thread([&](void){ - auto thing = currentWorld->goInsideStructure(player); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - } - } else if (SDL_KEY == controlMap[1]) { - if (!fadeEnable) { - player->vel.x = -PLAYER_SPEED_CONSTANT; - if (std::stoi(game::getValue("Slow")) == 1) - player->vel.x /= 2.0f; - player->left = left = true; - player->right = right = false; - if (currentWorldToLeft) { - std::thread([&](void){ - auto thing = currentWorld->goWorldLeft(player); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - } - } - } else if (SDL_KEY == controlMap[2]) { - if (!fadeEnable) { - player->vel.x = PLAYER_SPEED_CONSTANT; - if (std::stoi(game::getValue("Slow")) == 1) - player->vel.x /= 2.0f; - player->right = right = true; - player->left = left = false; - if (currentWorldToRight) { - std::thread([&](void){ - auto thing = currentWorld->goWorldRight(player); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - } - } - } else if (SDL_KEY == controlMap[3]) { - if (game::canSprint) { - if (debug) { - Mix_PlayChannel(1, sanic, -1); - player->speed = 4.0f; - } else { - player->speed = 2.0f; - } - } - } else if (SDL_KEY == controlMap[4]) { - player->speed = .5; - } else if (SDL_KEY == controlMap[5]) { - edown = true; - - // start hover counter? - if (!heyOhLetsGo) { - heyOhLetsGo = game::time::getTickCount(); - player->inv->mouseSel = false; - } - - // run hover thing - if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(player->inv->invOpen) && !(player->inv->selected)) { - player->inv->invHover = true; - - // enable action ui - action::enable(); - } - } else switch(SDL_KEY) { - case SDLK_DELETE: - gameRunning = false; - break; - case SDLK_t: - game::time::tick(50); - break; - default: - break; - } - } - break; - /* - * KEYUP - */ - - case SDL_KEYUP: - if (SDL_KEY == SDLK_ESCAPE) { - ui::menu::toggle(); - player->save(); - return; - } else if (SDL_KEY == controlMap[1]) { - left = false; - } else if (SDL_KEY == controlMap[2]) { - right = false; - } else if (SDL_KEY == controlMap[3]) { - if (player->speed == 4) - Mix_FadeOutChannel(1, 2000); - - player->speed = 1; - } else if (SDL_KEY == controlMap[4]) { - player->speed = 1; - } else if (SDL_KEY == controlMap[5]) { - edown = false; - - if (player->inv->invHover) { - player->inv->invHover = false; - } else { - if (!player->inv->selected) - player->inv->invOpening ^= true; - else - player->inv->selected = false; - - player->inv->mouseSel = false; - } - - // disable action ui - action::disable(); - - heyOhLetsGo = 0; - } else if (SDL_KEY == SDLK_q) { - auto item = player->inv->getCurrentItem(); - if (item != nullptr) { - if (player->inv->takeItem(item->name, 1) == 0) - currentWorld->addObject(item->name, "o shit waddup", - player->loc.x + player->width / 2, player->loc.y + player->height / 2); - } - } else if (SDL_KEY == SDLK_h) { - quest::toggle(); - } else switch (SDL_KEY) { - case SDLK_F3: - debug ^= true; - break; - case SDLK_BACKSLASH: - dialogBoxExists = false; - break; - case SDLK_x: - m = currentWorld->getNearMob(*player); - if (m != nullptr) - m->ride(player); - break; - case SDLK_i: - if (indoor && indoor->isFloorAbove(player)) { - player->loc.y += INDOOR_FLOOR_HEIGHT; - player->ground = false; - } - break; - case SDLK_k: - if (indoor && indoor->isFloorBelow(player)) { - player->loc.y -= INDOOR_FLOOR_HEIGHT; - player->ground = false; - } - break; - case SDLK_l: - currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); - currentWorld->getLastLight().follow(player); - currentWorld->getLastLight().makeFlame(); - break; - case SDLK_f: - currentWorld->addLight({player->loc.x, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); - break; - case SDLK_b: - if (debug) - posFlag ^= true; - else { - auto s = new Structures(); - s->spawn(FIRE_PIT, player->loc.x, player->loc.y); - //currentWorld->addStructure(s); - //currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y}, 400.0f, {1.0f,1.0f,1.0f}); - //currentWorld->getLastLight()->follow(currentWorld->build.back()); - //currentWorld->getLastLight()->makeFlame(); - } - break; - case SDLK_F12: - // Make the BYTE array, factor of 3 because it's RBG. - static GLubyte* pixels; - pixels = new GLubyte[ 3 * SCREEN_WIDTH * SCREEN_HEIGHT]; - glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels); - takeScreenshot(pixels); - - std::cout << "Took screenshot" << std::endl; - break; - case SDLK_UP: - player->inv->setSelectionUp(); - break; - case SDLK_DOWN: - player->inv->setSelectionDown(); - break; - default: - break; - } - - if (!left&&!right) - player->vel.x=0; - - break; - default: - break; - } - } - // Flush preloaded AI functions if necessary if (!dialogBoxExists) { while (!aipreload.empty()) { @@ -1637,7 +1294,7 @@ EXIT: void drawFade(void) { auto SCREEN_WIDTH = game::SCREEN_WIDTH; auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; - + if (!fadeIntensity) { if (fontSize != 16) setFontSize(16); @@ -1658,22 +1315,20 @@ EXIT: offset.x + SCREEN_WIDTH / 2, offset.y - SCREEN_HEIGHT / 2, -7.9, offset.x - SCREEN_WIDTH / 2 - 1, offset.y + SCREEN_HEIGHT / 2, -7.9, offset.x + SCREEN_WIDTH / 2, offset.y + SCREEN_HEIGHT / 2, -7.9}; - + setFontZ(-8.2); - glUniform1i(textShader_uniform_texture, 0); - glUseProgram(textShader); + glUniform1i(Render::textShader.uniform[WU_texture], 0); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, backdrop); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, backdrop); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); + Render::textShader.disable(); + Render::textShader.unuse(); - glUseProgram(0); setFontZ(-8.0); } @@ -1788,3 +1443,188 @@ EXIT: fclose(bmp); } } + +using namespace ui; + +void InputSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)en; + (void)ev; + (void)dt; + + + auto SCREEN_WIDTH = game::SCREEN_WIDTH; + auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; + + auto indoor = dynamic_cast<IndoorWorld *>(currentWorld); + Mob *m; // ;lkjfdsa + Entity *ent; // used for interaction + + SDL_Event e; + + // update mouse coords + mouse.x = premouse.x + offset.x - (SCREEN_WIDTH / 2); + mouse.y = (offset.y + SCREEN_HEIGHT / 2) - premouse.y; + + static vec2 fr; + static Entity *ig; + + while(SDL_PollEvent(&e)) { + switch(e.type) { + + // escape - quit game + case SDL_QUIT: + gameRunning = false; + break; + + // mouse movement - update mouse vector + case SDL_MOUSEMOTION: + premouse.x=e.motion.x; + premouse.y=e.motion.y; + break; + + case SDL_MOUSEBUTTONUP: + if (ig) { + ig->vel.x = (fr.x - mouse.x) / 50.0f; + ig->vel.y = (fr.y - mouse.y) / 50.0f; + //ig->forcedMove = true; // kills vel.x too quickly + ig = NULL; + } + break; + + // mouse clicks + case SDL_MOUSEBUTTONDOWN: + + // run actions? + if ((action::make = e.button.button & SDL_BUTTON_RIGHT)) + /*player->inv->invHover =*/ edown = false; + + textToDraw.clear(); + + if (dialogBoxExists || pageTexReady) { + // right click advances dialog + if ((e.button.button & SDL_BUTTON_RIGHT)) + dialogAdvance(); + } else { + // left click uses item + if (e.button.button & SDL_BUTTON_LEFT) { + if ((ent = currentWorld->getNearMob(*player)) != nullptr) { + player->inv->currentAddInteract(ent); + } + player->inv->useCurrent(); + } + + } + + if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width && + mouse.y > player->loc.y && mouse.y < player->loc.y + player->height) { + player->vel.y = .05; + fr = mouse; + ig = player; + } else { + for (auto &e : currentWorld->entity) { + if (mouse.x > e->loc.x && mouse.x < e->loc.x + e->width && + mouse.y > e->loc.y && mouse.y < e->loc.y + e->height) { + e->vel.y = .05; + fr = mouse; + ig = e; + break; + } + } + } + + break; + + case SDL_MOUSEWHEEL: + ev.emit<MouseScrollEvent>(e.wheel.y); + break; + + // key presses + case SDL_KEYDOWN: + ev.emit<KeyDownEvent>(SDL_KEY); + break; + /* + * KEYUP + */ + + case SDL_KEYUP: + ev.emit<KeyUpEvent>(SDL_KEY); + + if (SDL_KEY == SDLK_q) { + auto item = player->inv->getCurrentItem(); + if (item != nullptr) { + if (player->inv->takeItem(item->name, 1) == 0) + currentWorld->addObject(item->name, "o shit waddup", + player->loc.x + player->width / 2, player->loc.y + player->height / 2); + } + } else if (SDL_KEY == SDLK_h) { + quest::toggle(); + } else switch (SDL_KEY) { + case SDLK_F3: + debug ^= true; + break; + case SDLK_BACKSLASH: + dialogBoxExists = false; + break; + case SDLK_x: + m = currentWorld->getNearMob(*player); + if (m != nullptr) + m->ride(player); + break; + case SDLK_i: + if (indoor && indoor->isFloorAbove(player)) { + player->loc.y += INDOOR_FLOOR_HEIGHT; + player->ground = false; + } + break; + case SDLK_k: + if (indoor && indoor->isFloorBelow(player)) { + player->loc.y -= INDOOR_FLOOR_HEIGHT; + player->ground = false; + } + break; + case SDLK_l: + currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); + currentWorld->getLastLight().follow(player); + currentWorld->getLastLight().makeFlame(); + break; + case SDLK_f: + currentWorld->addLight({player->loc.x, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); + break; + case SDLK_b: + if (debug) + posFlag ^= true; + else { + auto s = new Structures(); + s->spawn(FIRE_PIT, player->loc.x, player->loc.y); + //currentWorld->addStructure(s); + //currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y}, 400.0f, {1.0f,1.0f,1.0f}); + //currentWorld->getLastLight()->follow(currentWorld->build.back()); + //currentWorld->getLastLight()->makeFlame(); + } + break; + case SDLK_F12: + // Make the BYTE array, factor of 3 because it's RBG. + static GLubyte* pixels; + pixels = new GLubyte[ 3 * SCREEN_WIDTH * SCREEN_HEIGHT]; + glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels); + takeScreenshot(pixels); + + std::cout << "Took screenshot" << std::endl; + break; + case SDLK_UP: + player->inv->setSelectionUp(); + break; + case SDLK_DOWN: + player->inv->setSelectionDown(); + break; + default: + break; + } + + break; + default: + break; + } + } +} diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp index ca830d8..f0299c6 100644 --- a/src/ui_menu.cpp +++ b/src/ui_menu.cpp @@ -1,5 +1,7 @@ #include <ui_menu.hpp> +#include <render.hpp> + #include <fstream> extern bool gameRunning; @@ -196,10 +198,7 @@ namespace ui { SDL_Event e; - useShader(&textShader, - &textShader_uniform_texture, - &textShader_attribute_coord, - &textShader_attribute_tex); + Render::useShader(&Render::textShader); setFontSize(24); game::config::update(); @@ -231,7 +230,7 @@ namespace ui { static GLuint backTex = Texture::genColor(Color(0, 0, 0, 204)); //static GLuint bsTex = Texture::genColor(Color(30, 30, 30)); - static GLuint border = Texture::genColor(Color(245, 245, 245)); + static GLuint border = Texture::genColor(Color(245, 245, 245)); GLfloat line_tex[] = {0.0, 0.0, 1.0, 0.0, @@ -241,13 +240,13 @@ namespace ui { //draw the dark transparent background glColor4f(0.0f, 0.0f, 0.0f, .8f); - glUseProgram(textShader); - + Render::textShader.use(); + glBindTexture(GL_TEXTURE_2D, backTex); - drawRect(vec2(offset.x - SCREEN_WIDTH / 2 - 1, offset.y - (SCREEN_HEIGHT / 2)), - vec2(offset.x + SCREEN_WIDTH / 2, offset.y + (SCREEN_HEIGHT / 2)), -8.5); + Render::drawRect(vec2(offset.x - SCREEN_WIDTH / 2 - 1, offset.y - (SCREEN_HEIGHT / 2)), + vec2(offset.x + SCREEN_WIDTH / 2, offset.y + (SCREEN_HEIGHT / 2)), -8.5); - glUseProgram(0); + Render::textShader.unuse(); //loop through all elements of the menu for (auto &m : currentMenu->items) { @@ -256,12 +255,12 @@ namespace ui { //draw the button background GLuint bsTex = Texture::genColor(Color(m.button.color.red,m.button.color.green,m.button.color.blue)); - - glUseProgram(textShader); + + Render::textShader.use(); glBindTexture(GL_TEXTURE_2D, bsTex); - drawRect(vec2(offset.x + m.button.loc.x, offset.y + m.button.loc.y), - vec2(offset.x + m.button.loc.x + m.button.dim.x, offset.y + m.button.loc.y + m.button.dim.y), -8.6); + Render::drawRect(vec2(offset.x + m.button.loc.x, offset.y + m.button.loc.y), + vec2(offset.x + m.button.loc.x + m.button.dim.x, offset.y + m.button.loc.y + m.button.dim.y), -8.6); //draw the button text putStringCentered(offset.x + m.button.loc.x + (m.button.dim.x/2), (offset.y + m.button.loc.y + (m.button.dim.y/2)) - ui::fontSize/2, @@ -273,24 +272,23 @@ namespace ui { //if the mouse if over the button, it draws this white outline glBindTexture(GL_TEXTURE_2D, border); - + GLfloat verts[] = {offset.x+m.button.loc.x, offset.y+m.button.loc.y, -8.7, offset.x+m.button.loc.x+m.button.dim.x, offset.y+m.button.loc.y, -8.7, offset.x+m.button.loc.x+m.button.dim.x, offset.y+m.button.loc.y+m.button.dim.y, -8.7, offset.x+m.button.loc.x, offset.y+m.button.loc.y+m.button.dim.y, -8.7, offset.x+m.button.loc.x, offset.y+m.button.loc.y, -8.7}; - glUseProgram(textShader); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); + Render::textShader.use(); + Render::textShader.enable(); - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, verts); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); glDrawArrays(GL_LINE_STRIP, 0, 5); - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); - + Render::textShader.disable(); + Render::textShader.unuse(); + //if the mouse is over the button and clicks if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { switch(m.member) { @@ -330,25 +328,25 @@ namespace ui { m.slider.sliderLoc = m.slider.minValue + (*m.slider.var/m.slider.maxValue)*(m.slider.dim.x-sliderW); } //draw the background of the slider - glUseProgram(textShader); + Render::textShader.use(); GLuint bsTex = Texture::genColor(Color(m.slider.color.red, m.slider.color.green, m.slider.color.blue, 175)); - GLuint hTex = Texture::genColor(Color(m.slider.color.red, m.slider.color.green, m.slider.color.blue, 255)); + GLuint hTex = Texture::genColor(Color(m.slider.color.red, m.slider.color.green, m.slider.color.blue, 255)); glBindTexture(GL_TEXTURE_2D, bsTex); - drawRect(vec2(offset.x + m.slider.loc.x, offset.y + m.slider.loc.y), - vec2(offset.x + m.slider.loc.x + m.slider.dim.x, offset.y + m.slider.loc.y + m.slider.dim.y), -8.6); - + Render::drawRect(vec2(offset.x + m.slider.loc.x, offset.y + m.slider.loc.y), + vec2(offset.x + m.slider.loc.x + m.slider.dim.x, offset.y + m.slider.loc.y + m.slider.dim.y), -8.6); + //draw the slider handle glBindTexture(GL_TEXTURE_2D, hTex); if (m.slider.dim.y > m.slider.dim.x) { - drawRect(vec2(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), - vec2(offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.7); + Render::drawRect(vec2(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), + vec2(offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.7); //draw the now combined slider text putStringCentered(offset.x + m.slider.loc.x + (m.slider.dim.x/2), (offset.y + m.slider.loc.y + (m.slider.dim.y*1.05)) - ui::fontSize/2, outSV); }else{ - drawRect(vec2(offset.x+m.slider.loc.x+m.slider.sliderLoc, offset.y+m.slider.loc.y), - vec2(offset.x+m.slider.loc.x + m.slider.sliderLoc + sliderW, offset.y+m.slider.loc.y + sliderH), -8.7); + Render::drawRect(vec2(offset.x+m.slider.loc.x+m.slider.sliderLoc, offset.y+m.slider.loc.y), + vec2(offset.x+m.slider.loc.x + m.slider.sliderLoc + sliderW, offset.y+m.slider.loc.y + sliderH), -8.7); //draw the now combined slider text putStringCentered(offset.x + m.slider.loc.x + (m.slider.dim.x/2), (offset.y + m.slider.loc.y + (m.slider.dim.y/2)) - ui::fontSize/2, outSV); @@ -359,20 +357,19 @@ namespace ui { //if it is we draw a white border around it glBindTexture(GL_TEXTURE_2D, border); - glUseProgram(textShader); - glEnableVertexAttribArray(textShader_attribute_coord); - glEnableVertexAttribArray(textShader_attribute_tex); - + Render::textShader.use(); + Render::textShader.enable(); + GLfloat box_border[] = {offset.x+m.slider.loc.x, offset.y+m.slider.loc.y, -8.8, offset.x+m.slider.loc.x+m.slider.dim.x, offset.y+m.slider.loc.y, -8.8, offset.x+m.slider.loc.x+m.slider.dim.x, offset.y+m.slider.loc.y+m.slider.dim.y, -8.8, offset.x+m.slider.loc.x, offset.y+m.slider.loc.y+m.slider.dim.y, -8.8, offset.x+m.slider.loc.x, offset.y+m.slider.loc.y, -8.8}; - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, box_border); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); - glDrawArrays(GL_LINE_STRIP, 0, 5); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, box_border); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); + glDrawArrays(GL_LINE_STRIP, 0, 5); if (m.slider.dim.y > m.slider.dim.x) { //and a border around the slider handle GLfloat handle_border[] = {offset.x+m.slider.loc.x, static_cast<GLfloat>(offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), -8.8, @@ -380,9 +377,9 @@ namespace ui { offset.x+m.slider.loc.x + sliderW, static_cast<GLfloat>(offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.8, offset.x+m.slider.loc.x, static_cast<GLfloat>(offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.8, offset.x+m.slider.loc.x, static_cast<GLfloat>(offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), -8.8}; - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, handle_border); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); - glDrawArrays(GL_LINE_STRIP, 0, 5); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, handle_border); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); + glDrawArrays(GL_LINE_STRIP, 0, 5); }else{ //and a border around the slider handle GLfloat handle_border[] = {offset.x+m.slider.loc.x + m.slider.sliderLoc, offset.y+m.slider.loc.y, -8.8, @@ -390,14 +387,13 @@ namespace ui { offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW), offset.y+m.slider.loc.y+m.slider.dim.y,-8.8, offset.x+m.slider.loc.x + m.slider.sliderLoc, offset.y+m.slider.loc.y+m.slider.dim.y, -8.8, offset.x+m.slider.loc.x + m.slider.sliderLoc, offset.y+m.slider.loc.y, -8.8}; - glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, handle_border); - glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); - glDrawArrays(GL_LINE_STRIP, 0, 5); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, handle_border); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, line_tex); + glDrawArrays(GL_LINE_STRIP, 0, 5); } - glDisableVertexAttribArray(textShader_attribute_coord); - glDisableVertexAttribArray(textShader_attribute_tex); + Render::textShader.disable(); //if we are inside the slider and click it will set the slider to that point if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) { @@ -406,15 +402,15 @@ namespace ui { *m.slider.var = (((mouse.y-offset.y) - m.slider.loc.y)/m.slider.dim.y)*100; //draw a white box over the handle glBindTexture(GL_TEXTURE_2D, border); - drawRect(vec2(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), - vec2(offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.9); + Render::drawRect(vec2(offset.x+m.slider.loc.x, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05)), + vec2(offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH), -8.9); }else{ *m.slider.var = (((mouse.x-offset.x) - m.slider.loc.x)/m.slider.dim.x)*100; //draw a white box over the handle glBindTexture(GL_TEXTURE_2D, border); - drawRect(vec2(offset.x+m.slider.loc.x + m.slider.sliderLoc, offset.y+m.slider.loc.y), - vec2(offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW), offset.y+m.slider.loc.y + m.slider.dim.y), -8.9); + Render::drawRect(vec2(offset.x+m.slider.loc.x + m.slider.sliderLoc, offset.y+m.slider.loc.y), + vec2(offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW), offset.y+m.slider.loc.y + m.slider.dim.y), -8.9); } } diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..4a060de --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,71 @@ +#include <window.hpp> + +#include <config.hpp> + +#include <SDL2/SDL_image.h> +#include <SDL2/SDL_mixer.h> + +constexpr const char* WINDOW_TITLE = "gamedev"; + +WindowSystem::WindowSystem(void) +{ + // attempt to initialize SDL + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) { + std::cout << "SDL was not able to initialize! Error: " << SDL_GetError(); + abort(); + } + atexit(SDL_Quit); + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + + // attempt to initialize SDL_image + if (!(IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) & (IMG_INIT_PNG | IMG_INIT_JPG))) { + std::cout << "Could not init image libraries! Error: " << IMG_GetError(); + abort(); + } + atexit(IMG_Quit); + + // attempt to initialize SDL_mixer + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { + std::cout << "SDL_mixer could not initialize! Error: " << Mix_GetError(); + abort(); + } + Mix_AllocateChannels(8); + atexit(Mix_Quit); + + // create the SDL window object + window = SDL_CreateWindow(WINDOW_TITLE, + SDL_WINDOWPOS_UNDEFINED, // Spawn the window at random (undefined) x and y coordinates + SDL_WINDOWPOS_UNDEFINED, // + game::SCREEN_WIDTH, + game::SCREEN_HEIGHT, + SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL + ); + + if (window == nullptr) { + std::cout << "The window failed to generate! SDL_Error: " << SDL_GetError(); + abort(); + } + + // create the OpenGL object that SDL provides + if ((glContext = SDL_GL_CreateContext(window)) == nullptr) { + std::cout << "The OpenGL context failed to initialize! SDL_Error: " << SDL_GetError(); + abort(); + } +} + +WindowSystem::~WindowSystem(void) +{ + SDL_GL_DeleteContext(glContext); + SDL_DestroyWindow(window); +} + +void WindowSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)en; + (void)ev; + (void)dt; + + SDL_GL_SwapWindow(window); +} diff --git a/src/world.cpp b/src/world.cpp index 418ce31..3871867 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -15,6 +15,8 @@ #include <ui.hpp> #include <gametime.hpp> +#include <render.hpp> + // local library headers #include <tinyxml2.h> using namespace tinyxml2; @@ -23,8 +25,8 @@ void makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions( unsigned size, void *coordAddr, void *texAddr, unsigned triCount ) { - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, size, coordAddr); - glVertexAttribPointer(worldShader_attribute_tex , 2, GL_FLOAT, GL_FALSE, size, texAddr ); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, size, coordAddr); + glVertexAttribPointer(Render::worldShader.tex , 2, GL_FLOAT, GL_FALSE, size, texAddr ); glDrawArrays(GL_TRIANGLES, 0, triCount); } @@ -32,13 +34,11 @@ void makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions_Ju unsigned size, void *coordAddr, void *texAddr, unsigned triCount ) { - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(size, coordAddr, texAddr, triCount); - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_coord); + Render::worldShader.disable(); } /* ---------------------------------------------------------------------------- @@ -277,7 +277,7 @@ void World::drawBackgrounds(void) } glActiveTexture(GL_TEXTURE0); - glUniform1i(worldShader_uniform_texture, 0); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); // draw background images. GLfloat tex_coord[] = { 0.0f, 1.0f, @@ -330,24 +330,23 @@ void World::drawBackgrounds(void) offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.8f, offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.8f}; - glUseProgram(worldShader); - glUniform1f(worldShader_uniform_light_impact, 0.0f); - glUniform4f(worldShader_uniform_ambient, 1.0, 1.0, 1.0, 1.0); + Render::worldShader.use(); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.0f); + glUniform4f(Render::worldShader.uniform[WU_ambient], 1.0, 1.0, 1.0, 1.0); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); bgTex(0); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, back_tex_coord, scrolling_tex_coord, 6); bgTex++; - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha)/255.0f); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha)/255.0f); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, fron_tex_coord, tex_coord, 6); @@ -407,17 +406,17 @@ void World::drawBackgrounds(void) } glBindTexture(GL_TEXTURE_2D, starTex); //glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, (255.0f - (randGet() % 200 - 100)) / 255.0f); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha)/255.0f); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha)/255.0f); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(5 * sizeof(GLfloat), &star_coord[0], &star_coord[3], star.size() * 6); } - glDisableVertexAttribArray(worldShader_attribute_coord); - glDisableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.disable(); + + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::worldShader.uniform[WU_ambient], ambient.red, ambient.green, ambient.blue, 1.0); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); - glUniform4f(worldShader_uniform_ambient, ambient.red, ambient.green, ambient.blue, 1.0); - glUseProgram(0); + Render::worldShader.unuse(); std::vector<vec3> bg_items; @@ -451,12 +450,12 @@ void World::drawBackgrounds(void) } } - glUseProgram(worldShader); - glUniform1f(worldShader_uniform_light_impact, 0.01); + Render::worldShader.use(); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.01); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions_JustDrawThis(0, &bg_i[0], &bg_tx[0], bg_items.size()); - glUseProgram(0); + Render::worldShader.unuse(); // draw the remaining layers for (unsigned int i = 0; i < 4; i++) { @@ -490,12 +489,12 @@ void World::drawBackgrounds(void) } } - glUseProgram(worldShader); - glUniform1f(worldShader_uniform_light_impact, 0.075f + (0.2f*i)); + Render::worldShader.use(); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.075f + (0.2f*i)); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions_JustDrawThis(0, &bg_i[0], &bg_tx[0], c.size()); - glUseProgram(0); + Render::worldShader.unuse(); } } @@ -541,13 +540,13 @@ void World::draw(Player *p) lightColors[lcIndex++] = 1.0; } - glUseProgram(worldShader); + Render::worldShader.use(); - glUniform4fv(worldShader_uniform_light, ls, lightCoords); - glUniform4fv(worldShader_uniform_light_color, ls, lightColors); - glUniform1i(worldShader_uniform_light_amt, ls); + glUniform4fv(Render::worldShader.uniform[WU_light], ls, lightCoords); + glUniform4fv(Render::worldShader.uniform[WU_light_color], ls, lightColors); + glUniform1i(Render::worldShader.uniform[WU_light_size], ls); - glUseProgram(0); + Render::worldShader.unuse(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -603,21 +602,17 @@ void World::draw(Player *p) dirtt.push_back(v.first.y); } - glUseProgram(worldShader); - glUniform1f(worldShader_uniform_light_impact, 0.45f); + Render::worldShader.use(); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 0.45f); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, &dirtc[0]); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, &dirtt[0]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, &dirtc[0]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, &dirtt[0]); glDrawArrays(GL_TRIANGLES, 0 , c.size()); - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_coord); - - glUseProgram(0); - + Render::worldShader.disable(); + Render::worldShader.unuse(); //glEnd(); @@ -687,51 +682,21 @@ void World::draw(Player *p) grasst.push_back(v.first.y); } - glUseProgram(worldShader); - glUniform1f(worldShader_uniform_light_impact, 1.0f); + Render::worldShader.use(); + glUniform1f(Render::worldShader.uniform[WU_light_impact], 1.0f); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, &grassc[0]); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, &grasst[0]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, &grassc[0]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, &grasst[0]); glDrawArrays(GL_TRIANGLES, 0 , c.size()); - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_coord); - - glUseProgram(0); - - - //glUseProgram(0); - //glDisable(GL_TEXTURE_2D); - - // draw particles - - glBindTexture(GL_TEXTURE_2D, colorIndex); - glUniform1i(worldShader_uniform_texture, 0); - glUseProgram(worldShader); - - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); - - /*GLfloat *pIndexT = &partVec[0]; - for (auto &p : particles) { - if (!p.behind) - p.draw(pIndexT); - } - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[3]); - glDrawArrays(GL_TRIANGLES, 0, ps * 6); -*/ - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_coord); - - glUseProgram(0); + Render::worldShader.disable(); + Render::worldShader.unuse(); for (auto &e :entity) e->draw(); - + // flatten grass under the player if the player is on the ground if (p->ground) { pOffset = (p->loc.x + p->width / 2 - worldStart) / HLINE; @@ -748,13 +713,12 @@ void World::draw(Player *p) // draw particles like a MASTAH glBindTexture(GL_TEXTURE_2D, colorIndex); - glUniform1i(worldShader_uniform_texture, 0); - glUseProgram(worldShader); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + Render::worldShader.use(); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, .8); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, .8); - glEnableVertexAttribArray(worldShader_attribute_coord); - glEnableVertexAttribArray(worldShader_attribute_tex); + Render::worldShader.enable(); partMutex.lock(); uint ps = particles.size(); @@ -774,16 +738,15 @@ void World::draw(Player *p) } partMutex.unlock(); - glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]); - glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[3]); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[3]); glDrawArrays(GL_TRIANGLES, 0, ps * 6); - glDisableVertexAttribArray(worldShader_attribute_tex); - glDisableVertexAttribArray(worldShader_attribute_coord); + Render::worldShader.disable(); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); - glUseProgram(0); + Render::worldShader.unuse(); } /** @@ -1442,7 +1405,7 @@ void World:: addMerchant(float x, float y, bool housed) { Merchant *tmp = new Merchant(); - + tmp->spawn(x, y); if (housed) { @@ -1675,7 +1638,7 @@ draw(Player *p) } */ - glUseProgram(worldShader); + Render::worldShader.use(); glActiveTexture(GL_TEXTURE0); bgTex(0); @@ -1683,8 +1646,8 @@ draw(Player *p) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //for the t direction - glUniform1i(worldShader_uniform_texture, 0); - glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); GLfloat backTile[] = {worldStart - SCREEN_WIDTH / 2, 0, 9.9, -worldStart + SCREEN_WIDTH / 2, 0, 9.9, @@ -1763,11 +1726,9 @@ draw(Player *p) } } - glUseProgram(worldShader); - + Render::worldShader.use(); makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions_JustDrawThis(0, &fc[0], &ft[0], floor.size() * 6); - - glUseProgram(0); + Render::worldShader.unuse(); /* * Draw all entities. diff --git a/xml/!town.xml b/xml/!town.xml index 2ffed91..b105127 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -4,8 +4,8 @@ <generation type="Random" width="1600"/> <time>6000</time> <spawnx>-300</spawnx> - <npc name="Sanc" hasDialog="true" health="1" x="507.65619" y="67.099007" dindex="0"/> - <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="-146.74963" y="61.999046" dindex="0"/> + <npc name="Sanc" hasDialog="true" health="1" x="406.31683" y="72.998955" dindex="9999"/> + <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="403.52261" y="72.998955" dindex="0"/> <structure type="1" spawnx="300" alive="1"/> <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/> <chest alive="1"/> |