Browse Source

move SDL_Handles reference to render_state

master
cinnaboot 8 years ago
parent
commit
8d83a2999b
  1. 50
      src/gooey.cpp
  2. 4
      src/gooey.h
  3. 17
      src/hexgame.cpp
  4. 12
      src/renderer.cpp
  5. 4
      src/renderer.h

50
src/gooey.cpp

@ -1,4 +1,6 @@
#include <cassert>
#if defined (_WIN32)
#include <SDL.h>
#else
@ -13,7 +15,7 @@
#include "gooey.h"
bool
initGooey(SDL_Handles &handles, v2i vp_dims /*TODO: pass in game state*/)
initGooey(SDL_Handles &handles, v2i vp_dims)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@ -46,9 +48,10 @@ gooeyProcessEvent(SDL_Event &event)
}
void
renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
hex_info* start_hex, hex_info* current_hex, bool &is_selecting, v3f camera_pos)
renderGooey(SDL_Handles &handles, hexgrid* grid, bool &is_debug, camera* cam)
{
assert(grid != nullptr);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(handles.window);
ImGui::NewFrame();
@ -63,32 +66,30 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
ImGui::SetNextWindowPos(ImVec2(0,0));
ImGui::SetNextWindowSize(ImVec2(300, 720));
ImGui::SetNextWindowBgAlpha(0.3f);
bool show_window;
ImGui::Begin("", &show_window, window_flags);
ImGui::Begin("", nullptr, window_flags);
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("%.3f ms/frame, (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::Text("Draw Mode:");
if (ImGui::RadioButton("None", (mode == NONE)))
mode = NONE;
if (ImGui::RadioButton("Fill", (mode == FILL)))
mode = FILL;
if (ImGui::RadioButton("Line", (mode == LINE)))
mode = LINE;
if (ImGui::RadioButton("Cone Fill", (mode == CONE_FILL)))
mode = CONE_FILL;
if (ImGui::RadioButton("None", (grid->draw_mode == NONE)))
grid->draw_mode = NONE;
if (ImGui::RadioButton("Fill", (grid->draw_mode == FILL)))
grid->draw_mode = FILL;
if (ImGui::RadioButton("Line", (grid->draw_mode == LINE)))
grid->draw_mode = LINE;
if (ImGui::RadioButton("Cone Fill", (grid->draw_mode == CONE_FILL)))
grid->draw_mode = CONE_FILL;
ImGui::Checkbox("Debug Render", &is_debug);
ImGui::SameLine(); ImGui::TextUnformatted(is_debug ? "true" : "false");
ImGui::Text("is_selecting");
ImGui::SameLine(); ImGui::TextUnformatted(is_selecting ? "true" : "false");
ImGui::SameLine(); ImGui::TextUnformatted(grid->is_selecting ? "true" : "false");
ImGui::SetNextTreeNodeOpen(true);
if (ImGui::CollapsingHeader("Camera Position")) {
ImGui::Text("x: %f", camera_pos.x);
ImGui::Text("y: %f", camera_pos.y);
ImGui::Text("z: %f", camera_pos.z);
if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text("x: %f", cam->position.x);
ImGui::Text("y: %f", cam->position.y);
ImGui::Text("z: %f", cam->position.z);
}
#if 0
@ -98,14 +99,12 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
#endif
//ImGui::ShowMetricsWindow();
if (current_hex)
{
Hex ch = current_hex->hex;
if (grid->current_hex) {
Hex ch = grid->current_hex->hex;
ImGui::Text("current_hex: %i, %i, %i", ch.q, ch.r, ch.s);
}
if (start_hex)
{
Hex sh = start_hex->hex;
if (grid->start_hex) {
Hex sh = grid->start_hex->hex;
ImGui::Text("start_hex: %i, %i, %i", sh.q, sh.r, sh.s);
}
@ -114,4 +113,3 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

4
src/gooey.h

@ -1,6 +1,7 @@
#pragma once
#include "camera.h"
#include "hexgame.h"
#include "hexgrid.h"
#include "renderer.h"
@ -8,6 +9,5 @@
bool initGooey(SDL_Handles &handles, v2i vp_dims);
void shutdownGooey();
bool gooeyProcessEvent(SDL_Event &event);
void renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
hex_info* start_hex, hex_info* current_hex, bool &is_selecting, v3f camera_pos);
void renderGooey(SDL_Handles &handles, hexgrid* grid, bool &is_debug, camera* cam);

17
src/hexgame.cpp

@ -483,19 +483,17 @@ int main(int argc, char* argv[])
g_render_state->is_debug_draw = DEBUG_DRAW;
g_render_state->viewport_dims = v2i(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
SDL_Handles handles;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
LOG(ERROR) << "Error, SDL_Init: " << SDL_GetError() << "\n";
return 1;
}
if (!initRenderer(g_render_state, handles)) {
if (!initRenderer(g_render_state)) {
LOG(ERROR) << "Unable to initialize graphics, exiting\n";
return 1;
}
if (!initGooey(handles, g_render_state->viewport_dims)) {
if (!initGooey(g_render_state->handles, g_render_state->viewport_dims)) {
LOG(ERROR) << "Fooey, No Gooey!\n";
return 1;
}
@ -513,7 +511,7 @@ int main(int argc, char* argv[])
// pre-load textures
const char * path = "../data/coords.layout.flat.png";
if (!addTexture(handles, path))
if (!addTexture(g_render_state->handles, path))
{
LOG(ERROR) << "Error adding " << path << "\n";
return 1;
@ -562,7 +560,7 @@ int main(int argc, char* argv[])
#if defined(_WIN32)
LOG(DEBUG) << "TODO: Test SDL_Delay frame timer in win32\n";
if (!platform_init(handles.window)) {
if (!platform_init(g_render_state->handles.window)) {
LOG(ERROR) << "Couldn't get SDL platform info, exiting\n";
return 1;
}
@ -588,10 +586,9 @@ int main(int argc, char* argv[])
if (r->is_debug_draw && g->grid.draw_mode == CONE_FILL)
renderDebug(r, g_polygon_select_vertices);
renderGooey(handles, g->grid.draw_mode, r->is_debug_draw, g->grid.start_hex,
g->grid.current_hex, g->grid.is_selecting, getCameraPosition());
renderGooey(g_render_state->handles, &g->grid, r->is_debug_draw, &renGetCamera());
SDL_GL_SwapWindow(handles.window);
SDL_GL_SwapWindow(g_render_state->handles.window);
// TODO: test SDL_Delay on Win32
//platform_wait_for_vblank(VSYNC_ENABLED);
@ -601,7 +598,7 @@ int main(int argc, char* argv[])
SDL_Delay(FRAME_DELAY - frameTime);
}
cleanUp(handles);
cleanUp(g_render_state->handles);
LOG(INFO) << "Application exiting\n";
return 0;
}

12
src/renderer.cpp

@ -74,7 +74,7 @@ static camera g_camera;
// TODO: maybe make this a contructor for render_state?
bool
initRenderer(render_state* rs, SDL_Handles &handles)
initRenderer(render_state* rs)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@ -83,19 +83,19 @@ initRenderer(render_state* rs, SDL_Handles &handles)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GetCurrentDisplayMode(0, &handles.currentDisplayMode);
handles.window =
SDL_GetCurrentDisplayMode(0, &rs->handles.currentDisplayMode);
rs->handles.window =
SDL_CreateWindow("hexgame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
rs->viewport_dims.x, rs->viewport_dims.y, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
if (!handles.window) {
if (!rs->handles.window) {
LOG(ERROR) << "Error creating window: " << SDL_GetError() << "\n";
return false;
}
handles.glContext = SDL_GL_CreateContext(handles.window);
rs->handles.glContext = SDL_GL_CreateContext(rs->handles.window);
if (!handles.glContext) {
if (!rs->handles.glContext) {
LOG(ERROR) << "Error creating glContext: " << SDL_GetError() << "\n";
return false;
}

4
src/renderer.h

@ -30,7 +30,7 @@ struct render_state
{
v2i viewport_dims;
bool is_debug_draw;
// TODO: maybe add handles reference here?
SDL_Handles handles;
// TODO: testing lighting
rg_point_light* lights = nullptr;
@ -38,7 +38,7 @@ struct render_state
uint max_lights = 0;
};
bool initRenderer(render_state* rs, SDL_Handles &handles);
bool initRenderer(render_state* rs);
void freeBuffers(render_state* rs);
bool addTexture(SDL_Handles &handles, const char * path);
camera& renGetCamera();

Loading…
Cancel
Save