diff options
Diffstat (limited to 'src/renderer/RSoftwareBackend.cpp')
| -rw-r--r-- | src/renderer/RSoftwareBackend.cpp | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/src/renderer/RSoftwareBackend.cpp b/src/renderer/RSoftwareBackend.cpp index d05cee7..587ac74 100644 --- a/src/renderer/RSoftwareBackend.cpp +++ b/src/renderer/RSoftwareBackend.cpp @@ -1,4 +1,3 @@ -#include "imgui.h" #include <renderer/RSoftwareBackend.hpp> #include <SDL3/SDL_video.h> @@ -25,33 +24,19 @@ RSoftwareBackend::RSoftwareBackend(Renderer& renderer) glGenTextures(1, &m_gltexture_id); glBindTexture(GL_TEXTURE_2D, m_gltexture_id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); -} - -void -RSoftwareBackend::ResizeCanvas(int32_t w, int32_t h) -{ - size_t realloc_size = (size_t)(w * h) * sizeof(m_canvas.pixels[0]); - void *realloc_data = realloc(m_canvas.pixels, realloc_size); - if (!realloc_data) { - printf("could not resize offscreen buffer\n"); - return; - } - - m_canvas.w = w; - m_canvas.h = h; - m_canvas.pixels = (uint32_t*)realloc_data; + 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); } void RSoftwareBackend::Draw() { - ResizeCanvas(m_renderer.m_screen_w, m_renderer.m_screen_h); - SortRenderEntities(); + Resize(m_renderer.m_screen_w, m_renderer.m_screen_h); + DrawClear(); + SortRenderEntities(); for (RSortEntry sort_entry : m_renderer.m_sort_entries) { REntity& entity = m_renderer.m_render_entities[sort_entry.entity_index]; @@ -76,7 +61,6 @@ RSoftwareBackend::Draw() } - glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_gltexture_id); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_canvas.w, m_canvas.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_canvas.pixels); @@ -86,8 +70,23 @@ RSoftwareBackend::Draw() glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); +} + +void +RSoftwareBackend::Resize(int32_t w, int32_t h) +{ + size_t realloc_size = (size_t)(w * h) * sizeof(m_canvas.pixels[0]); + void *realloc_data = realloc(m_canvas.pixels, realloc_size); + if (!realloc_data) { + printf("could not resize offscreen buffer\n"); + return; + } - glBindTexture(GL_TEXTURE_2D, 0); + m_canvas.w = w; + m_canvas.h = h; + m_canvas.pixels = (uint32_t*)realloc_data; + + glViewport(0, 0, w, h); } void @@ -101,6 +100,25 @@ RSoftwareBackend::SortRenderEntities() } void +RSoftwareBackend::DrawClear() +{ + Color color = m_renderer.m_clear_color; + uint32_t rshift = m_canvas.rshift; + uint32_t gshift = m_canvas.gshift; + uint32_t bshift = m_canvas.bshift; + + for (int32_t y = 0; y < m_canvas.h; y++) { + for (int32_t x = 0; x < m_canvas.w; x++) { + uint32_t r = (uint32_t)(color.r * 255.0f); + uint32_t g = (uint32_t)(color.g * 255.0f); + uint32_t b = (uint32_t)(color.b * 255.0f); + uint32_t val = r << rshift | g << gshift | b << bshift; + m_canvas.pixels[y*m_canvas.w + x] = val; + } + } +} + +void RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity) { int32_t xmin = m_renderer.WorldXToScreenX(entity.rect.x0); |
