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;
}
Related
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().
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.
I am trying to use a texture with the SDL_PIXELFORMAT_ARGB2101010 color format but I only get a black texture. The same program works when using the SDL_PIXELFORMAT_ARG88888 format.
None of SDL's variables are null. This is my code:
#include <SDL2/SDL.h>
int main(int argc, const char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, 0);
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB2101010, SDL_TEXTUREACCESS_STREAMING, bufferWidth, bufferHeight);
uint32_t* buffer = new uint32_t[bufferWidth * bufferHeight];
for(int bufferIdx = 0; bufferIdx < bufferWidth * bufferHeight; bufferIdx++) {
buffer[bufferIdx] = 0xc0ffffff;
}
SDL_Event event;
while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
running = false;
}
SDL_UpdateTexture(tex, nullptr, buffer, bufferWidth * 4);
SDL_RenderCopy(ren, tex, nullptr, nullptr);
SDL_RenderPresent(ren);
}
}
This is some of my hardware and software:
AMD Radeon Pro 5500M GPU
SDL Version 2.0.14
MacOS 11.1
C++14
Compiled with default Xcode 12.3 settings
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.
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.