SDL_Texture from IMG_Load() fails to draw? - c++

The only thing I see is the RenderDrawColor.
Also the "circle.png" is in the right folder (where the main.cpp is).
#include <SDL.h>
#include <SDL_Image.h>
int main(int argc,char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
SDL_Window* window = SDL_CreateWindow("_", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(render,0, 255, 255, 255);
SDL_Surface* img = IMG_Load("circle.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(render, img);
SDL_FreeSurface(img);
SDL_Event event;
while (1) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return 0;
}
}
SDL_RenderClear(render);
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
}
system("PAUSE");
return 0;
}

Had to include zlib1.dll and libpng16-16.dll to the same folder as the cpp.
libpng16-16.dll depends on zlib1.dll.

Related

SDL rect not appearing

I am trying to get an SDL_Rect to appear on screen with a texture from a bitmap. I run this program and the screen is simply white, with no image.
#include "SDL.h"
int main(int argc, char** args) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = NULL;
window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface* surface = SDL_LoadBMP("car.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 1280;
rect.h = 720;
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I got it working, I simply had to add SDL_RenderPresent().

SDL2 SDL_GetRenderDrawColor only able to display black

I am able to make a window with SDL2 and C++ that has a black background, as soon as I change one of the arguments from SDL_GetRenderDrawColor from 0 to anything else I get this error:
Error (active) E0167 argument of type "int" is incompatible with
parameter of type "Uint8 *"
this is my code:
#include <stdio.h>
#include <SDL.h>
#undef main
int main(int argc, char** argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window * window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
bool quit = false;
while (!quit) {
SDL_Delay(10);
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT: quit = true; break;
}
SDL_GetRenderDrawColor(renderer, 255, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I am working with Visual Studio 2019 Community, I already succesfully set up SDL2 in Visual Studio at my Workplace but doing the same things at home yielded this error.
If you want to set the render draw color, you should use SDL_SetRenderDrawColor. SDL_GetRenderDrawColor is used to get the render draw color.

From SMFL to SDL 2.0

Here is some code in SMFL
RenderWindow window(VideoMode(320, 480), "The Game!");
Texture t1,t2,t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
Sprite s(t1), background(t2), frame(t3);
Does SDL 2.0 has functions like this and how to convert them to SDL 2.0
yes, all is there:
https://programmersranch.blogspot.kr/2014/03/sdl2-animations-with-sprite-sheets.html
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Window * window = SDL_CreateWindow("SDL2 Sprite Sheets",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * image = IMG_Load("spritesheet.png");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,
image);
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}

Change background of SDL2 window?

I can create a SDL2 window but I don't know how to change background color of this window.
My code:
#include "SDL.h"
SDL_Window *window;
void main()
{
window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Delay(3000);
}
How can I change background color of this window to black?
You should set drawing colour with SDL_SetRenderDrawColor and then use SDL_RenderClear:
(code comes directly from SDL wiki)
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Renderer* renderer;
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
0);
// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, 0);
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
// Give us time to see the window.
SDL_Delay(5000);
// Always be sure to clean up
SDL_Quit();
return 0;
}
If you do not use SDL2 renderer you can do this:
Just call:
SDL_GL_SwapWindow(window);
After :
SDL_GL_MakeCurrent(window, context);
That's all! Now you have a black screen.
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(){
int width=512, height=512;
SDL_Window *window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
if(window==NULL)
cout << "There was an error while initializing the window" << endl << SDL_GetError << endl;
SDL_Event event;
bool running=true;
while(running){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
running=false;
break;
}
}
SDL_GetWindowSize(window, &width, &height);
SDL_Surface *surface=SDL_GetWindowSurface(window);
Uint32 skyblue=SDL_MapRGB(surface->format, 65,193,193);
SDL_FillRect(surface, NULL, skyblue);
SDL_UpdateWindowSurface(window);
}
SDL_DestroyWindow(window);
SDL_Quit();
}
Note, none of these solutions work properly with OpenGL. The question was how to init the window with predefined color, not how to paint the first frame. There's still a blink of white color before the first frame is painted.
Try this one :
SDL_Window *wind;
SDL_Surface *windSurface = NULL
void main()
{
wind = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windSurface = SDL_GetWindowSurface(wind);
SDL_FillRect(windSurface, NULL, SDL_MapRGB(windSurface->format, 240, 200, 112));
SDL_UpdateWindowSurface(wind);
SDL_Delay(3000);
}

SDL_Renderer won't show textures after passing through the function

I have a little problem with SDL_Renderer. I can't understand why it doesn't work. Let's look at this example, it works fine:
bool running = true;
SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* texture = IMG_LoadTexture(renderer, "asdf.bmp");
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
break;
default:
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
Then it comes to classes and when I pass the renderer through functions, it won't work anymore.
class Sprite
{
public:
Sprite(const std::string& path) : filePath(path) {};
~Sprite() { SDL_DestroyTexture(tex); };
void draw(SDL_Renderer* renderer);
private:
const std::string& filePath;
SDL_Texture* tex;
};
void Sprite::draw(SDL_Renderer* renderer)
{
printf("renderer sprite = %p\n", renderer);
tex = IMG_LoadTexture(renderer, filePath.c_str());
SDL_RenderCopy(renderer, tex, NULL, NULL);
}
int main(int argc, char **argv)
{
bool running = true;
SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event;
Sprite* sprite = new Sprite("asdf.bmp");
while(running)
{
printf("renderer main = %p\n", renderer);
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
break;
default:
break;
}
}
SDL_RenderClear(renderer);
sprite->draw(renderer);
SDL_RenderPresent(renderer);
}
return 0;
}
The address of renderer is the same in main and in the draw function. I know that I'm probably making some sort of beginner's mistake here but i can't find it out.
The texture is being loaded every frame when you call sprite->draw.
You should move the line tex = IMG_LoadTexture(renderer, filePath.c_str()); to the constructor so that it is only loaded once.
The reason for this is that the texture will not be ready for rendering in the same frame that it is loaded.
As a side note you don't appear to be cleaning up the SDL_Window or SDL_Renderer with their respective destroy functions or calling SDL_Quit although I accept this may have been omitted for submitting the code example.