#include "screen.h"
#include "SDL_opengl.h"
+#include <fcntl.h> // for access(2)
+#include <unistd.h> // for access(2)
using namespace IPDF;
using namespace std;
m_gl_context = SDL_GL_CreateContext(m_window);
- glClearColor(1.f,1.f,1.f,1.f);
- glClear(GL_COLOR_BUFFER_BIT);
+ ResizeViewport(800, 600);
+
+ Clear();
Present();
- ResizeViewport(800, 600);
}
SDL_Quit();
}
+void Screen::Clear(float r, float g, float b, float a)
+{
+ glClearColor(r,g,b,a);
+ glClear(GL_COLOR_BUFFER_BIT);
+}
+
void Screen::ResizeViewport(int width, int height)
{
glViewport(0, 0, width, height);
int w = ViewportWidth();
int h = ViewportHeight();
- unsigned char * pixels = new unsigned char[w*h*3];
+ unsigned char * pixels = new unsigned char[w*h*4];
if (pixels == NULL)
- Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*3);
+ Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4);
+
- glReadPixels(0,0,w, h, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
+ for (int y = 0; y < h; ++y)
+ {
+ glReadPixels(0,h-y-1,w, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[y*w*4]);
+ }
- SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*3, w*3, 0,0,0,0);
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0x000000ff,0x0000ff00,0x00ff0000,0xff000000);
+#else
+ SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
+#endif
if (surf == NULL)
Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError());
*/
void Screen::RenderBMP(const char * filename) const
{
+ if (access(filename, R_OK) == -1)
+ {
+ Error("No such file \"%s\" - Nothing to render - You might have done this deliberately?", filename);
+ return;
+ }
SDL_Surface * bmp = SDL_LoadBMP(filename);
if (bmp == NULL)
Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError());