From 4e2dd51ad1e36edcdf7975a6eb1242460f950cf6 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 11 Apr 2021 21:28:40 +0200 Subject: [PATCH] add opengl texture from sdl_surface --- framework/opengl_primitives/CMakeLists.txt | 2 ++ .../src/mm/opengl/texture.hpp | 2 ++ .../src/mm/opengl/texture_loader.cpp | 27 ++++++++++++++++++- .../src/mm/opengl/texture_loader.hpp | 7 +++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/framework/opengl_primitives/CMakeLists.txt b/framework/opengl_primitives/CMakeLists.txt index f2aa1eb..d1c3bf8 100644 --- a/framework/opengl_primitives/CMakeLists.txt +++ b/framework/opengl_primitives/CMakeLists.txt @@ -11,6 +11,8 @@ target_include_directories(opengl_primitives PUBLIC "${CMAKE_CURRENT_SOURCE_DIR} target_link_libraries(opengl_primitives glm stb_image + #sdl + sdl_service logger resource_manager diff --git a/framework/opengl_primitives/src/mm/opengl/texture.hpp b/framework/opengl_primitives/src/mm/opengl/texture.hpp index 21d19fd..e7e06d8 100644 --- a/framework/opengl_primitives/src/mm/opengl/texture.hpp +++ b/framework/opengl_primitives/src/mm/opengl/texture.hpp @@ -6,6 +6,7 @@ namespace MM::OpenGL { struct TextureLoaderFile; struct TextureLoaderConstBuffer; + struct TextureLoaderSDLSurface; class Texture { private: @@ -14,6 +15,7 @@ namespace MM::OpenGL { private: friend struct TextureLoaderFile; friend struct TextureLoaderConstBuffer; + friend struct TextureLoaderSDLSurface; Texture( uint32_t handle, diff --git a/framework/opengl_primitives/src/mm/opengl/texture_loader.cpp b/framework/opengl_primitives/src/mm/opengl/texture_loader.cpp index 971bce7..2cb8718 100644 --- a/framework/opengl_primitives/src/mm/opengl/texture_loader.cpp +++ b/framework/opengl_primitives/src/mm/opengl/texture_loader.cpp @@ -8,7 +8,6 @@ #include #endif - #include #include @@ -129,5 +128,31 @@ std::shared_ptr TextureLoaderConstBuffer::load(const uint8_t* data, siz return std::shared_ptr(new Texture(handle, width, height, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE)); } +std::shared_ptr TextureLoaderSDLSurface::load(SDL_Surface* surface) const { + uint32_t handle; + + glGenTextures(1, &handle); + glBindTexture(GL_TEXTURE_2D, handle); + + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + SDL_LockSurface(surface); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + glBindTexture(GL_TEXTURE_2D, 0); + + SDL_UnlockSurface(surface); + + return std::shared_ptr(new Texture(handle, surface->w, surface->h, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE)); +} + } // MM::OpenGL diff --git a/framework/opengl_primitives/src/mm/opengl/texture_loader.hpp b/framework/opengl_primitives/src/mm/opengl/texture_loader.hpp index 91858b6..6786ca4 100644 --- a/framework/opengl_primitives/src/mm/opengl/texture_loader.hpp +++ b/framework/opengl_primitives/src/mm/opengl/texture_loader.hpp @@ -1,6 +1,9 @@ #pragma once #include "./texture.hpp" + +#include + #include // fwd @@ -18,6 +21,10 @@ namespace MM::OpenGL { std::shared_ptr load(const uint8_t* data, size_t size) const; }; + struct TextureLoaderSDLSurface final { + std::shared_ptr load(SDL_Surface* surface) const; + }; + struct TextureLoaderEmpty final { template std::shared_ptr load(Args&& ... args) const {