spacebox/src/Display.cpp

68 lines
1.8 KiB
C++

#include "Display.hpp"
#include "Game.hpp"
Display::Display(Node* parent) : Node(parent) {}
glm::ivec2 Display::get_window_size()
{
glm::ivec2 size;
SDL_GetWindowSize(get_root()->window, &size.x, &size.y);
return size;
}
void Display::get_screen_pixels(unsigned char* pixels, int w, int h, int x, int y)
{
if (get_root()->is_gl_context)
{
GLenum format;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
format = GL_RGBA;
#else
format = GL_BGRA;
#endif
glReadPixels(x, y, w, h, format, GL_UNSIGNED_BYTE, pixels);
}
else
{
Uint32 format;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
format = SDL_PIXELFORMAT_ABGR8888;
#else
format = SDL_PIXELFORMAT_ARGB8888;
#endif
SDL_RenderReadPixels(
get_root()->renderer, NULL, format, pixels, bpp / 8 * w);
}
}
SDL_Surface* Display::get_screen_surface()
{
glm::ivec2 size = get_window_size();
unsigned char* pixels = new unsigned char[bpp / 8 * size.x * size.y];
get_screen_pixels(pixels, size.x, size.y);
SDL_Surface* surface = get_screen_surface_from_pixels(
pixels, get_root()->is_gl_context);
delete[] pixels;
return surface;
}
SDL_Surface* Display::get_screen_surface_from_pixels(
unsigned char* pixels, bool flip)
{
glm::ivec2 size = get_window_size();
SDL_Surface* surface;
if (flip)
{
SDL_Surface* pixel_surface = SDL_CreateRGBSurfaceFrom(
pixels, size.x, size.y, bpp, bpp / 8 * size.x, 0, 0, 0, 0);
surface = zoomSurface(pixel_surface, 1, -1, SMOOTHING_OFF);
SDL_FreeSurface(pixel_surface);
}
else
{
surface = SDL_CreateRGBSurface(0, size.x, size.y, bpp, 0, 0, 0, 0);
std::memcpy(surface->pixels, pixels, bpp / 8 * size.x * size.y);
}
return surface;
}