spacebox/src/Display.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

2019-05-07 03:33:54 -04:00
#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;
}
2019-05-16 03:51:36 -04:00
void Display::get_screen_pixels(unsigned char* pixels, int w, int h, int x, int y)
{
2019-06-16 03:05:02 -04:00
if (get_root()->is_gl_context)
{
GLenum format;
2019-05-16 03:51:36 -04:00
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
2019-06-16 03:05:02 -04:00
format = GL_RGBA;
2019-05-16 03:51:36 -04:00
#else
2019-06-16 03:05:02 -04:00
format = GL_BGRA;
2019-05-16 03:51:36 -04:00
#endif
2019-06-16 03:05:02 -04:00
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);
}
2019-05-16 03:51:36 -04:00
}
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);
2019-06-16 03:05:02 -04:00
SDL_Surface* surface = get_screen_surface_from_pixels(
pixels, get_root()->is_gl_context);
2019-05-22 04:27:15 -04:00
delete[] pixels;
return surface;
}
2019-06-16 03:05:02 -04:00
SDL_Surface* Display::get_screen_surface_from_pixels(
unsigned char* pixels, bool flip)
2019-05-22 04:27:15 -04:00
{
glm::ivec2 size = get_window_size();
2019-06-16 03:05:02 -04:00
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;
2019-05-16 03:51:36 -04:00
}