Browse Source

added SDL_image for loading png/jpeg files

master
cinnaboot 8 years ago
parent
commit
724549b542
  1. BIN
      data/coords.layout.flat.png
  2. 4
      src/Makefile
  3. 11
      src/gooey.h
  4. 22
      src/hexgame.cpp
  5. 28
      src/renderer.h

BIN
data/coords.layout.flat.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

4
src/Makefile

@ -21,6 +21,10 @@ OBJECTS += $(IMGUI_DIR)/imgui.o $(IMGUI_DIR)/imgui_demo.o $(IMGUI_DIR)/imgui_dra
# don't know a good way to require this in make without forcing a specific version
LDFLAGS = -lSDL2 -lGLEW -lGL
# SDL2_image
CXXFLAGS += $(shell pkg-config --cflags SDL2_image)
LDFLAGS += $(shell pkg-config --libs SDL2_image)
# TODO: only needed for gl3w, remove later
LDFLAGS += -ldl

11
src/gooey.h

@ -51,7 +51,6 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
{
ImGui_ImplSdlGL3_NewFrame(handles.window);
#if 1
ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoTitleBar;
window_flags |= ImGuiWindowFlags_NoScrollbar;
@ -60,11 +59,10 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
window_flags |= ImGuiWindowFlags_NoCollapse;
ImGui::SetNextWindowPos(ImVec2(0,0));
ImGui::SetNextWindowSize(ImVec2(300, 250));
ImGui::SetNextWindowSize(ImVec2(300, 550));
ImGui::SetNextWindowBgAlpha(0.3f);
bool show_window;
ImGui::Begin("", &show_window, window_flags);
#endif
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("%.3f ms/frame, (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
@ -84,6 +82,11 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
ImGui::Text("is_selecting");
ImGui::SameLine(); ImGui::TextUnformatted(is_selecting ? "true" : "false");
// testing SDL_image
SDL_Surface* image = handles.texSurfaces[0];
ImGui::Image((void*)(intptr_t) image->userdata, ImVec2(image->w, image->h));
//ImGui::ShowMetricsWindow();
if (current_hex)
{
Hex ch = current_hex->hex;
@ -96,10 +99,8 @@ renderGooey(SDL_Handles &handles, HexDrawMode &mode, bool &is_debug,
}
ImGui::End();
ImGui::Render();
ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData());
}
#endif // GOOEY_H

22
src/hexgame.cpp

@ -19,6 +19,7 @@
#endif
#include <SDL.h>
#include <SDL_image.h>
#include "hexgame.h"
#include "hexlib.h"
@ -352,6 +353,7 @@ cleanUp(SDL_Handles &handles)
freeBuffers(); // renderer.h
for (SDL_Surface *surface : handles.texSurfaces)
SDL_FreeSurface(surface);
IMG_Quit(); // SDL_image
SDL_GL_DeleteContext(handles.glContext);
SDL_DestroyWindow(handles.window);
SDL_Quit();
@ -407,6 +409,26 @@ int main(int argc, char* argv[])
LOG(ERROR) << "Unable to initialize graphics, exiting\n";
return 1;
}
{
// load support for the JPG and PNG image formats
int flags=IMG_INIT_JPG|IMG_INIT_PNG;
int initted=IMG_Init(flags);
if((initted&flags) != flags)
{
LOG(ERROR) << "IMG_Init: Failed to init required jpg and png support!\n";
LOG(ERROR) << "IMG_Init: " << IMG_GetError() << "\n";
return 1;
}
// pre-load textures
std::string path = "../data/coords.layout.flat.png";
if (!addTexture(handles, path))
{
LOG(ERROR) << "Error adding " << path << "\n";
return 1;
}
}
if (!createScene(g_game_state->hex_array))
{

28
src/renderer.h

@ -182,6 +182,34 @@ initRenderer(SDL_Handles &handles, v2i vpDims)
return true;
}
bool
addTexture(SDL_Handles &handles, std::string path)
{
// testing
LOG(INFO) << "Loading image: " << path << "\n";
SDL_Surface* image = IMG_Load(path.c_str());
if (!image)
{
LOG(ERROR) << "IMG_Load: " << IMG_GetError() << "\n";
return 1;
}
GLuint tex_id;
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// store opengl id in SDL_Surface.userdat
image->userdata = (void*)(intptr_t) tex_id;
handles.texSurfaces.push_back(image);
return true;
}
// NOTE: mat_id should match the matrix variable name in the shader source
bool
initShaderProgram(gl_render_group &rg, const char * vertex_code, const char * frag_code, const char * mat_id)

Loading…
Cancel
Save