SDL_Rect is larger than specified - c++

I've gotten a weird error with SDL_Rect. Whenever I call SDL_RenderFillRect, with this rectangle:
SDL_Rect rect = {100, 100, 100, 100};
I always end up with the rectangle covering the entire 800 x 600 window.
Here's my program:
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
class Window
{
public:
Window( std::string title, int width, int height );
void runEvents( SDL_Event event );
void drawRect( int x, int y, int h, int w );
bool closed = false;
SDL_Renderer *_renderer;
SDL_Window *_window;
private:
int colour = 0;
};
Window::Window( std::string title, int width, int height )
{
_window = SDL_CreateWindow( title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0 );
_renderer = SDL_CreateRenderer( _window, -1, 0 );
}
// this function deals with rects (it's supposed to draw a rect when the mouse button is down):
void Window::runEvents( SDL_Event event )
{
SDL_Rect rect = { 100, 100, 100, 100 };
switch( event.type )
{
case SDL_QUIT:
closed = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
closed = true;
break;
case SDLK_KP_1:
// set colour to blue
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
std::cout << "Colour set to blue!" << std::endl;
colour = 1;
break;
case SDLK_KP_2:
// set colour to red
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
std::cout << "Colour set to red!" << std::endl;
colour = 2;
break;
case SDLK_KP_3:
// set colour to green
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
std::cout << "Colour set to green!" << std::endl;
colour = 3;
break;
case SDLK_KP_4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
std::cout << "Colour set to purple!" << std::endl;
colour = 4;
break;
case SDLK_KP_0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
std::cout << "Colour set to white!" << std::endl;
colour = 0;
break;
}
break;
//this draws the rectangle:
case SDL_MOUSEBUTTONDOWN:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
//this removes it
case SDL_MOUSEBUTTONUP:
switch( colour )
{
case 1:
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
break;
case 2:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 3:
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
break;
case 4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
break;
case 0:
SDL_SetRenderDrawColor( _renderer, 255, 255, 255, 255 );
break;
}
drawRect( rect.h, rect.y, rect.h, rect.w );
break;
}
}
//this function is supposed to draw the rect
void Window::drawRect( int x, int y, int h, int w )
{
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect( _renderer, &rect );
}
bool running = true;
void mouseCheck()
{
while( running )
{
int x;
int y;
SDL_GetMouseState( &x, &y );
printf( "Mouse is at x: %i and y: %i\n", x, y );
SDL_Delay( 5000 );
}
}
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
Window window( "Hello World", 800, 600 );
SDL_Event event;
SDL_SetRenderDrawColor( window._renderer, 255, 255, 255, 255 );
std::thread mousePosition( mouseCheck );
mousePosition.detach();
while( !window.closed )
{
SDL_RenderClear( window._renderer );
SDL_RenderPresent( window._renderer );
if( SDL_PollEvent( &event ) )
{
window.runEvents( event );
}
}
return 0;
}

Multiple issues:
Right now your main draw loop is:
1. Draw
2. Clear
3. Present
You want:
1. (re)Set clear color
2. Clear
3. Draw
4. Present
Not sure what you're trying for with that mouseCheck() thread but avoid calling GUI-related SDL functions from other threads. If you need that info in another thread use thread-safe atomics/queues/messages to communicate it.
You're calling drawRect() in two places with rect.h as the first parameter instead of rect.x.
I'd recommend against interleaving input processing and rendering like you're doing in runEvents(). Update the system state (shouldDrawRect & colour) while draining the event queue (while(SDL_PollEvent())), then draw a frame with the new state (Window::drawScene()).
You're only processing a single event per frame. Drain the event queue each frame via while(SDL_PollEvent()) so that events don't get backed up and dropped.
All together:
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
class Window
{
public:
Window( std::string title, int width, int height );
void runEvents( SDL_Event event );
void drawScene();
void drawRect( int x, int y, int h, int w );
bool closed = false;
SDL_Renderer *_renderer;
SDL_Window *_window;
private:
bool shouldDrawRect;
int colour = 0;
};
Window::Window( std::string title, int width, int height )
{
_window = SDL_CreateWindow( title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0 );
_renderer = SDL_CreateRenderer( _window, -1, 0 );
shouldDrawRect = false;
}
void Window::drawScene()
{
if( shouldDrawRect )
{
switch( colour )
{
case 0:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 1:
SDL_SetRenderDrawColor( _renderer, 0, 51, 204, 255 );
break;
case 2:
SDL_SetRenderDrawColor( _renderer, 255, 0, 0, 255 );
break;
case 3:
SDL_SetRenderDrawColor( _renderer, 51, 204, 51, 255 );
break;
case 4:
SDL_SetRenderDrawColor( _renderer, 153, 0, 153, 255 );
break;
}
SDL_Rect rect = { 100, 100, 100, 100 };
drawRect( rect.x, rect.y, rect.h, rect.w );
}
}
//this function deals with rects (it's supposed to draw a rect when the mouse button is down):
void Window::runEvents( SDL_Event event )
{
switch( event.type )
{
case SDL_QUIT:
closed = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
closed = true;
break;
case SDLK_KP_1:
// set colour to blue
std::cout << "Colour set to blue!" << std::endl;
colour = 1;
break;
case SDLK_KP_2:
// set colour to red
std::cout << "Colour set to red!" << std::endl;
colour = 2;
break;
case SDLK_KP_3:
// set colour to green
std::cout << "Colour set to green!" << std::endl;
colour = 3;
break;
case SDLK_KP_4:
std::cout << "Colour set to purple!" << std::endl;
colour = 4;
break;
case SDLK_KP_0:
std::cout << "Colour set to white!" << std::endl;
colour = 0;
break;
}
break;
//this draws the rectangle:
case SDL_MOUSEBUTTONDOWN:
shouldDrawRect = true;
break;
//this removes it
case SDL_MOUSEBUTTONUP:
shouldDrawRect = false;
break;
}
}
//this function is supposed to draw the rect
void Window::drawRect( int x, int y, int h, int w )
{
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect( _renderer, &rect );
}
bool running = true;
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
Window window( "Hello World", 800, 600 );
SDL_Event event;
while( !window.closed )
{
SDL_SetRenderDrawColor( window._renderer, 255, 255, 255, 255 );
SDL_RenderClear( window._renderer );
while( SDL_PollEvent( &event ) )
{
window.runEvents( event );
}
window.drawScene();
SDL_RenderPresent( window._renderer );
}
return 0;
}

Related

SDL window updates slowly if I don't move my mouse

I recently tried to implement OneLoneCoder's simple 3d Graphics Engine, from his Code-It-Yourself series, on SDL2. So I made some triangles that move on the window, but these triangles would appear to move at a slower framerate when I left my mouse completely still, and then move at their normal framerate if I started to move my mouse.
You see, to animate these triangles I used some while loops, something like this:
while (true) {
// Black Background
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Custom function that draws a triangle using SDL_RenderDrawLine()
drawTriangle(renderer, triangle);
SDL_RenderPresent(renderer); // updates the pixels on the renderer
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
goto Exit;
}
}
Pretty standard, right?
Here is an example of some code that has this exact same issue:
#include<iostream>
#include"SDL.h"
struct vec {
float x, y;
vec ( float x, float y)
: x(x), y(y)
{}
vec () {}
};
struct triangle {
vec p[3];
triangle() {}
};
void drawTriangle(SDL_Renderer* r, const triangle& tri) {
SDL_RenderDrawLine(r, tri.p[0].x, tri.p[0].y, tri.p[1].x, tri.p[1].y);
SDL_RenderDrawLine(r, tri.p[1].x, tri.p[1].y, tri.p[2].x, tri.p[2].y);
SDL_RenderDrawLine(r, tri.p[2].x, tri.p[2].y, tri.p[0].x, tri.p[0].y);
}
int main(int argc, char **argv) {
uint16_t width = 400;
uint16_t height = 300;
if (SDL_Init(SDL_INIT_VIDEO) > 0) { std::cout << "SDL_INIT FAILED\nERROR: " << SDL_GetError() << std::endl; }
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Event event;
window = SDL_CreateWindow("SDL Test :)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
if (window == NULL) { std::cout << "SDL_WINDOW FAILED\nERROR: " << SDL_GetError() << std::endl; }
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
triangle myTri;
myTri.p[0] = vec( width * 0.5, 0);
myTri.p[1] = vec( width * 0.333, height * 0.2);
myTri.p[2] = vec( width * 0.666, height * 0.2);
while (true) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
myTri.p[0].y += 0.01f;
myTri.p[1].y += 0.01f;
myTri.p[2].y += 0.01f;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
drawTriangle(renderer, myTri);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
goto Exit;
}
}
Exit:
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

SDL2 TTF text renders randomly disappearing

im using SDL2 and just grasped some of the basic-concepts of SDL TTF, but for some reason after a certain amount of time running the app, my text elements disappear with seemingly no reason why, can someone please tell me what i need to do to top my text renders from disappearing? what went wrong here?
#include <iostream>
#include <iomanip>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include "AdviLib.h"
#define SDLKCM SDLK_COMMA
#define SDLKPR SDLK_PERIOD
#define SDLKSP SDLK_SPACE
#define SDLKES SDLK_ESCAPE
void Screenshot(SDL_Renderer* r, const char* name, int w, int h)
{
const uint32_t format{ SDL_PIXELFORMAT_ARGB8888 };
SDL_Surface* sur{ SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, format) };
SDL_RenderReadPixels(r, NULL, format, sur->pixels, sur->pitch);
SDL_SaveBMP(sur, name);
SDL_FreeSurface(sur);
std::cout << name << " captured!\n";
}
int main(int args, char* argc[])
{
// before SDL init
std::cout << "enter the name of the session: "; string sesh{ sInput() };
std::cout << "enter the name of the local, non .bmp, image: "; string image{ sInput() };
// SDL init
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
TTF_Init();
const int W{ 800 }; const int H{ 750 };
SDL_Window* win{ SDL_CreateWindow(sesh.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN) };
SDL_Renderer* ren{ SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) };
SDL_Surface* img{ IMG_Load(image.c_str()) };
TTF_Font* font{ TTF_OpenFont("CRYSRG__.ttf", 40) };
SDL_Color color{ 205, 99, 35 };
SDL_Texture* tex{ SDL_CreateTextureFromSurface(ren, img) };
SDL_Event e;
// buffer variables
string Txt; string Tx2; string Tx3; string Tx4; string Tx5; string Tx6; string Tx7;
SDL_Surface* ts1 = nullptr; SDL_Surface* ts2 = nullptr; SDL_Surface* ts3 = nullptr; SDL_Surface* ts4 = nullptr; SDL_Surface* ts5 = nullptr; SDL_Surface* ts6 = nullptr; SDL_Surface* ts7 = nullptr;
SDL_Texture* tb1 = nullptr; SDL_Texture* tb2 = nullptr; SDL_Texture* tb3 = nullptr; SDL_Texture* tb4 = nullptr; SDL_Texture* tb5 = nullptr; SDL_Texture* tb6 = nullptr; SDL_Texture* tb7 = nullptr;
int w{ img->w }; int h{ img->h }; int x{ randomWithinRange(W - w) }; int y{ randomWithinRange(H - h) };
int tbW{ 0 }; int tbH{ 0 }; int X{ 2 }; int tbY{ 1 }; int tb2Y{ 32 }; int tb3Y{ 63 }; int tb4Y{ 94 }; int tb5Y{ 125 }; int tb6Y{ 156 }; int tb7Y{ 187 };
int ms{ 1 };
bool q{ false };
double ANG{ 0 }; int ANGI{ 0 };
// before main loop
// main loop
while (!q)
{
// before buffer setup
SDL_Rect IR{ x, y, w, h };
// buffer setup
ANGI = ANG;
int _TXT_RED = color.r; int _TXT_GRN = color.g; int _TXT_BLU = color.b;
//current x pos, current y pos, current angle, current speed, text color red, text color grn, text color blue,
char buf1[255]; char buf2[255]; char buf3[255]; char buf4[255]; char buf5[255]; char buf6[255]; char buf7[255];
sprintf_s(buf1, "x: %i", x); sprintf_s(buf2, "y: %i", y); sprintf_s(buf3, "a: %i", ANGI); sprintf_s(buf4, "MS: %i", ms); sprintf_s(buf5, "TR: %i", _TXT_RED); sprintf_s(buf6, "TG: %i", _TXT_GRN);
sprintf_s(buf7, "TB: %i", _TXT_BLU);
Txt = buf1; Tx2 = buf2; Tx3 = buf3; Tx4 = buf4; Tx5 = buf5; Tx6 = buf6; Tx7 = buf7;
ts1 = TTF_RenderText_Blended(font, Txt.c_str(), color); ts2 = TTF_RenderText_Blended(font, Tx2.c_str(), color); ts3 = TTF_RenderText_Blended(font, Tx3.c_str(), color);
ts4 = TTF_RenderText_Blended(font, Tx4.c_str(), color); ts5 = TTF_RenderText_Blended(font, Tx5.c_str(), color); ts6 = TTF_RenderText_Blended(font, Tx6.c_str(), color);
ts7 = TTF_RenderText_Blended(font, Tx7.c_str(), color);
tb1 = SDL_CreateTextureFromSurface(ren, ts1); tb2 = SDL_CreateTextureFromSurface(ren, ts2); tb3 = SDL_CreateTextureFromSurface(ren, ts3); tb4 = SDL_CreateTextureFromSurface(ren, ts4);
tb5 = SDL_CreateTextureFromSurface(ren, ts5); tb6 = SDL_CreateTextureFromSurface(ren, ts6); tb7 = SDL_CreateTextureFromSurface(ren, ts7);
SDL_QueryTexture(tb1, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb2, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb3, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb4, NULL, NULL, &tbW, &tbH);
SDL_QueryTexture(tb5, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb6, NULL, NULL, &tbW, &tbH); SDL_QueryTexture(tb7, NULL, NULL, &tbW, &tbH);
SDL_Rect Rct{ X, tbY, tbW, tbH }; SDL_Rect Rc2{ X, tb2Y, tbW, tbH }; SDL_Rect Rc3{ X, tb3Y, tbW, tbH }; SDL_Rect Rc4{ X, tb4Y, tbW, tbH }; SDL_Rect Rc5{ X, tb5Y, tbW, tbH }; SDL_Rect Rc6{ X, tb6Y, tbW, tbH };
SDL_Rect Rc7{ X, tb7Y, tbW, tbH };
// set display renders
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
SDL_RenderCopyEx(ren, tex, NULL, &IR, ANG, 0, SDL_FLIP_NONE);
SDL_RenderCopy(ren, tb1, NULL, &Rct); SDL_RenderCopy(ren, tb2, NULL, &Rc2); SDL_RenderCopy(ren, tb3, NULL, &Rc3); SDL_RenderCopy(ren, tb4, NULL, &Rc4); SDL_RenderCopy(ren, tb5, NULL, &Rc5);
SDL_RenderCopy(ren, tb6, NULL, &Rc6); SDL_RenderCopy(ren, tb7, NULL, &Rc7);
SDL_RenderPresent(ren);
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT) q = true;
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_w: y -= ms; break; case SDLK_s: y += ms; break;
case SDLK_a: x -= ms; break; case SDLK_d: x += ms; break;
case SDLK_r: if (color.r < 255) color.r += 1; else if (color.r == 255) color.r = 0; break;
case SDLK_g: if (color.g < 255) color.g += 1; else if (color.g == 255) color.g = 0; break;
case SDLK_b: if (color.b < 255) color.b += 1; else if (color.b == 255) color.b = 0; break;
case SDLKCM: ANG -= 1; break;
case SDLKPR: ANG += 1; break;
case SDLKSP: Screenshot(ren, "capture.bmp", W, H); break;
case SDLK_q: if (ms > 1) { ms -= 1; /*std::cout << "ms decreased to " << ms << "\n";*/ } /*else if (ms == 1) std::cout << "ms cannot decrease further\n";*/ break;
case SDLK_e: if (ms < 10) { ms += 1; /*std::cout << "ms increased to " << ms << "\n";*/ } /*else if (ms == 10) ms = 10; std::cout << "ms cannot increase further\n";*/ break;
case SDLKES: q = true; break;
}
}
}
}
// after main loop - usually nothing
// exit code
SDL_DestroyTexture(tb1); SDL_DestroyTexture(tb2); SDL_DestroyTexture(tb3); SDL_DestroyTexture(tb4); SDL_DestroyTexture(tb5); SDL_DestroyTexture(tb6); SDL_DestroyTexture(tb7); SDL_DestroyTexture(tex);
SDL_FreeSurface(ts1); SDL_FreeSurface(ts2); SDL_FreeSurface(ts3); SDL_FreeSurface(ts4); SDL_FreeSurface(ts5); SDL_FreeSurface(ts6); SDL_FreeSurface(ts7); SDL_FreeSurface(img);
TTF_CloseFont(font);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}
Its because you keep loading more textures and surfaces without freeing them. You only need to load them once. Put your buffer setup before the loop, also check the memory usage when its running im almost certain its gonna be rising alot

C++ SDL2: How to render rects into multiple viewports

I am new into SDL2(version 2.0.10) and I teach it from Lazy Foo tutorial. In Lesson of The ViewPort example code render me only first image in left viewport and others not. Render of rects in different viewports don't work either. What I am doing wrong when I want render rects in different viewports like this:
while( !quit ){
while( SDL_PollEvent( &e ) != 0 ){
if( e.type == SDL_QUIT ){
quit = true;
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Top left corner viewport
SDL_Rect topLeftViewport;
topLeftViewport.x = 0;
topLeftViewport.y = 0;
topLeftViewport.w = SCREEN_WIDTH / 2;
topLeftViewport.h = SCREEN_HEIGHT / 2;
SDL_RenderSetViewport( gRenderer, &topLeftViewport );
SDL_Rect fillRect = { 10, 10, 100, 100 };
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0x00, 0x00, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
SDL_Rect topRightViewport;
topRightViewport.x = SCREEN_WIDTH / 2;
topRightViewport.y = 0;
topRightViewport.w = SCREEN_WIDTH / 2;
topRightViewport.h = SCREEN_HEIGHT / 2;
SDL_RenderSetViewport( gRenderer, &topRightViewport );
SDL_Rect fillRect2 = { 10, 10, 100, 100 };
SDL_SetRenderDrawColor( gRenderer, 0x00, 0xFF, 0x00, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect2 );
SDL_RenderPresent( gRenderer );
}
As I saw, you are drawing a rect at 10, 10 of 100x100 two times, this coordinates doesn't take account of the viewport coordinates. And in a way, if you want to do this by changing the viewport clipping is not relevant... just draw the square where you want.
UPDATE: I tried multiples viewports. Here is a working example. You can choose between the renderer (with viewports) or the classic way.
main.cpp :
#include "Application.hpp"
int
main (int argc, char * argv[])
{
Application app("SDL 2 Test", 800, 600, true);
return app.mainLoop();
}
Application.hpp :
#ifndef APPLICATION_HPP
#define APPLICATION_HPP
#include <SDL2/SDL.h>
class Application
{
public:
Application (const char * title = "UnknownApplication", int baseWidth = 640, int baseHeight = 480, bool useRenderer = false) noexcept;
~Application ();
int mainLoop () noexcept;
private:
int m_width = 0;
int m_height = 0;
SDL_Window * m_window = nullptr;
SDL_Renderer * m_renderer = nullptr;
bool m_useRenderer = false;
bool m_isRunning = false;
};
#endif /* APPLICATION_HPP */
Application.cpp :
#include "Application.hpp"
#include <iostream>
#include <SDL2/SDL_image.h>
Application::Application (const char * title, int baseWidth, int baseHeight, bool useRenderer) noexcept
: m_width(baseWidth), m_height(baseHeight), m_useRenderer(useRenderer)
{
if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return;
}
m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_width, m_height, SDL_WINDOW_SHOWN);
if ( m_window == nullptr )
{
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
return;
}
if ( m_useRenderer )
{
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
if ( m_renderer == nullptr )
{
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
return;
}
}
m_isRunning = true;
}
int
Application::mainLoop () noexcept
{
SDL_Event event;
const auto filepath = "SET_IMAGE_YOU_WANT_HERE";
auto surface = IMG_Load(filepath);
if ( surface == nullptr )
{
std::cerr << "Unable to read image file : " << filepath << std::endl;
return 1;
}
SDL_Rect logoPosition = {8, 8, 32, 32};
if ( m_useRenderer )
{
auto texture = SDL_CreateTextureFromSurface(m_renderer, surface);
SDL_Rect screenA = {0, 0, m_width / 2, m_height / 2};
SDL_Rect screenB = {m_width / 2, 0, m_width / 2, m_height / 2};
while ( m_isRunning )
{
while ( SDL_PollEvent(&event) != 0 )
{
if ( event.type == SDL_QUIT )
m_isRunning = false;
}
SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255);
SDL_RenderClear(m_renderer);
SDL_RenderSetViewport(m_renderer, &screenA);
SDL_SetRenderDrawColor(m_renderer, 255, 0, 0, 255);
SDL_RenderFillRect(m_renderer, nullptr);
SDL_RenderCopy(m_renderer, texture, nullptr, &logoPosition);
SDL_RenderSetViewport(m_renderer, &screenB);
SDL_SetRenderDrawColor(m_renderer, 0, 255, 0, 255);
SDL_RenderFillRect(m_renderer, nullptr);
SDL_RenderCopy(m_renderer, texture, nullptr, &logoPosition);
SDL_RenderPresent(m_renderer);
}
SDL_DestroyTexture(texture);
}
else
{
auto windowSurface = SDL_GetWindowSurface(m_window);
while ( m_isRunning )
{
while ( SDL_PollEvent(&event) != 0 )
{
if ( event.type == SDL_QUIT )
m_isRunning = false;
}
SDL_FillRect(windowSurface, nullptr, SDL_MapRGB(windowSurface->format, 0xFF, 0x00, 0xFF));
//SDL_BlitSurface(surface, nullptr, windowSurface, &logoPosition);
SDL_BlitScaled(surface, nullptr, windowSurface, &logoPosition);
SDL_UpdateWindowSurface(m_window);
}
}
SDL_FreeSurface(surface);
return 0;
}
Application::~Application (void)
{
if ( m_renderer != nullptr )
SDL_DestroyRenderer(m_renderer);
if ( m_window != nullptr )
SDL_DestroyWindow(m_window);
SDL_Quit();
}

General SDL2 problems. MacOS, Xode

What I'm trying to do is make an SDL program that can write special symbols to a window I created. The keyboard can change between 4 layers each registering 32 keys. I just can't figure out how to actually make something appear on the screen and have no idea what I'm doing wrong.
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[])
{
//Setup
bool quit = false;
SDL_Event event;
int z = 0; //Layer
int x = 0;
int y = 0;
//Init
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("TPKB", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 960, 640, SDL_WINDOW_SHOWN);
SDL_Rect rect = {x, y, 32, 32};
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * A_srf = SDL_LoadBMP("A.bmp"); SDL_Texture * A = SDL_CreateTextureFromSurface(renderer, A_srf); SDL_FreeSurface(A_srf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
//Events
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYUP:
{
if(z == 0)
{
switch(event.key.keysym.sym)
{
//case SDLK_1: SDL_BlitSurface(A_srf, NULL, surface, srfrect); break;
case SDLK_2: SDL_RenderCopy(renderer, A, NULL, &rect); if(x == 928){x = 0; y += 32;} else{x += 32;} rect.x = x; rect.y = y; SDL_RenderClear(renderer); SDL_RenderPresent(renderer); break;
case SDLK_LEFT: z = 1; std::cout << "1"; break;
}
}
if(z == 1)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: z = 0; std::cout << "0"; break;
}
}
}
}
}
//Cleanup
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(A);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I've tried moving the render clear render present stuff around but it hasn't seemed to be activating at all.

Seg fault with Allegro bitmaps

I am using liballegro4.2-dev on Ubuntu 14.04 to (hopefully) write a simple game. My code compiles fine using the command g++ Picker.cpp -o out ``allegro-config --libs, but when I run the code I always get the error message Shutting down Allegro due to signal #11 Segmentation fault (core dumped). I suspect this has something to do with the line: masked_blit(face, screen, 0, 0, 0, 0, 64, 64);, but Internet searches turn nothing up and I am out of ideas. The full code is below:
#include <allegro.h>
void setUpAllegro()
{
//INIT
allegro_init();
install_keyboard();
install_timer();
install_mouse();
install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, 0 );
set_color_depth( 16 );
bool fullscreen = false;
if (fullscreen == true)
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
else
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
//END INIT
}
class Player
{
public:
int x;
int y;
void Move(int dir)
{
switch(dir)
{
case 1:
y += 5;
break;
case 2:
y -= 5;
break;
case 3:
x += 5;
break;
case 4:
x -= 5;
break;
default:
//exit(1);
break;
}
}
};
int main()
{
setUpAllegro();
Player player;
bool done = false;
BITMAP *buffer = create_bitmap( 640, 480 );
BITMAP *face;
face = load_bitmap("/home/alexander/Desktop/Pooper Picker/fake.BMP",NULL);
while (!done)
{
if( key[KEY_ESC] )
done = true;
if( key[KEY_UP])
player.Move(1);
else if( key[KEY_DOWN] )
player.Move(2);
if ( key[KEY_LEFT] )
player.Move(3);
else if( key[KEY_RIGHT] )
player.Move(4);
masked_blit(face, screen, 0, 0, 0, 0, 64, 64);
blit( buffer, screen, 0, 0, 5, 5, 640, 480 );
release_screen();
clear_bitmap( buffer );
}
//free memory
destroy_bitmap( buffer );
return 0;
}
END_OF_MAIN();
Thanks for the help!