I’m currently following LimeOats’s youtube tutorial on SDL (code link ), and have run into an issue with rendering sprites.
My code is pretty much the same as his except for the fact that I’m using Visual Studio 2022 (the headers are just “SDL.h” and “SDL_image.h.” I also deleted the “const” in main’s parameter because it was acting up).
The problem is, while the code compiles and runs (without giving me any errors in the terminal), the sprite is still not rendered on the screen. Any help would be appreciated since I’m relatively new to SDL2.
I dunno how to draw a rectangle? I thought that this was using the pixels from the png. I know that the filepath is correct though, I see it being loaded into the map when debugging. I checked another image and it doesn’t seem to work either. I’ll check the surface to texture out and see what happens.
Along with Levo’s suggestions, double-check your SDL_RenderCopy source rect and dest rect are initialized to appropriate values when you are drawing from a portion of the sprite sheet.
It is very easy to forget to set one of those values and miss either the screen or sprite sheet, or render a zero-sized or negative-sized rectangle.
I haven’t looked at a tutorial in forever but I am positive https://chatgpt.com/ can emit working code since there’s so much SDL code out there.
SDL draw a green rect white background
You’ll have to ask it how to do events and how to use SDL image to create a texture and draw. It’s unfortunate it generated C++ code since SDL is a C api. Actually look up each function in the header or wiki and understand what’s going on, otherwise you’ll have a bad time. SDL2/SDL_RenderFillRect - SDL Wiki
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char* argv[]) {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}
// Create a window
SDL_Window* window = SDL_CreateWindow("SDL Draw Green Rectangle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if (!window) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
// Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Set the draw color to white and clear the screen
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // White
SDL_RenderClear(renderer);
// Set the draw color to green
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Green
// Define the rectangle
SDL_Rect rect = { 100, 100, 200, 150 }; // x, y, width, height
// Draw the rectangle
SDL_RenderFillRect(renderer, &rect);
// Update the screen
SDL_RenderPresent(renderer);
// Wait for 5 seconds
SDL_Delay(5000);
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I try never to use ChatGPT but this was a great help! Maybe I am too pigheaded when it comes to that haha. Turns out that I didn’t initialize one of the sourceRectangle variables I was using, so that’s my bad, although it’s nice to have a better understanding of how it gets drawn from this. Thanks to both of you for spending time helping me out!