0

I am working with SDL3 and I am encountering an issue with rendering textures. I am able to create the textures, but they don't show up on the screen when I try to render them.

Here's what I'm trying to do:

I am creating two surfaces, converting them into textures, and trying to render one of them after applying a rotation.

The surface creation and texture creation seem to be working fine, but when I try to render the texture, it doesn't appear on the screen.

I have tried:

  • Ensuring that the renderer is set to clear the screen before rendering new textures.

  • Using SDL_RenderCopyEx() for rotating the texture, but no matter what, it doesn't render.

  • Debugging the issue by checking for errors after each step (e.g., SDL_CreateTextureFromSurface()), but everything seems fine.

Here's my code:

#include <SDL3/SDL.h>
#include <iostream>
#include <cmath>
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

float x = 100, w = 50, y = 100, h = 50;
float xb = 50, wb = 50, yb = 50, hb = 50;
bool bult = false;

int main() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {

        cerr << "SDL_Init failed: " << SDL_GetError() << endl;
        return 1;
    }

    SDL_Window* win1 = SDL_CreateWindow("SDL3 Window", 800, 600, SDL_WINDOW_OPENGL);

    if (!win1) {

        cerr << "Window creation failed: " << SDL_GetError() << endl;
        return 1;
    }
    SDL_Renderer* render1 = SDL_CreateRenderer(win1,NULL);
    if (!render1) {
        cerr << "render creation failed: " << SDL_GetError() << endl;
        return 1;
    }
    SDL_Surface* obg1 = SDL_CreateSurface(100, 100, SDL_PIXELFORMAT_ABGR32);
    SDL_FillSurfaceRect(obg1, NULL, 150);
    SDL_Texture* texture1 = SDL_CreateTextureFromSurface(render1, obg1);
    SDL_DestroySurface(obg1);


    SDL_Texture* texture2 = nullptr;

    SDL_Event event;

    bool running = true;

    while (running) {
        float mX, mY;
        SDL_GetMouseState(&mX, &mY);
        float dX = mX - (x + w / 2.0f);
        float dY = mY - (y + h / 2.0f);
        float angleplayer = atan2(dY, dX);
        float degrees = angleplayer * (180.0f / M_PI);
        float speed = 10.0f; // سرعت حرکت
        float vx = cos(angleplayer) * speed;
        float vy = sin(angleplayer) * speed;
        while(SDL_PollEvent(&event)) {

            if (event.type==SDL_EVENT_QUIT) {

                running = false;

            }
            else if (event.type==SDL_EVENT_KEY_DOWN) {

                if (event.key.key == SDLK_ESCAPE) {

                    running = false;

                }
                else if (event.key.key == SDLK_S) {
                    y += 10;
                }
                else if (event.key.key == SDLK_W) {
                    y -= 10;
                }
                else if (event.key.key == SDLK_D) {
                    x += 10;
                    
                }
                else if (event.key.key == SDLK_A) {
                    x -= 10;
                    
                }
            }
            else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
                if (event.button.button == SDL_BUTTON_RIGHT && !texture2) {
                    SDL_Surface* obg2 = SDL_CreateSurface(50, 50, SDL_PIXELFORMAT_ABGR32);
                    SDL_FillSurfaceRect(obg2, NULL, 77);
                    texture2 = SDL_CreateTextureFromSurface(render1, obg2);
                    SDL_DestroySurface(obg2);
                    bult = true;
                    if (!obg2) {
                        cerr << "Surface creation failed: " << SDL_GetError() << endl;
                    }
                    
                }
                else if (event.button.button == SDL_BUTTON_X2) {
                    
                }
            }
        }
        
        SDL_SetRenderDrawColor(render1, 100, 100, 100, 250);
        SDL_RenderClear(render1);

        SDL_SetRenderDrawColor(render1, 0, 0, 250, 250);
        SDL_FRect fild = { x,y,w,h };
        //SDL_RenderFillRect(render1, &fild);
        SDL_RenderTextureRotated(render1, texture1, NULL, &fild, degrees, NULL, SDL_FLIP_NONE);
        if (bult && texture2) {
            SDL_FRect fild1 = { xb,yb,wb,hb };
            //SDL_RenderTextureRotated(render1, texture2, NULL, &fild1, degrees, NULL, SDL_FLIP_NONE);
            SDL_RenderTextureRotated(render1, texture2, NULL, &fild, degrees, NULL, SDL_FLIP_NONE);
            yb += vy;
            xb += vx;

        }

        SDL_RenderPresent(render1);
        SDL_Delay(16);
    }
    SDL_DestroyRenderer(render1);
    SDL_DestroyWindow(win1);
    SDL_Quit();
    return 0;
}

Problem: The textures are created successfully, but they don't render to the screen when I try to draw them. I've confirmed the creation of the textures and the renderer, but still, the screen stays blank after I attempt to render.

I'm not sure where I'm going wrong. Does anyone have experience with rendering SDL textures? Could you suggest anything that might fix the issue?

8
  • 1
    IDK but considering that your problem is with textures the first thing I would do it check whether they have been created. Your code doesn't do that. Commented Apr 23 at 16:20
  • i tried this code and it works, how are you running this code ? which OS or platform ? could it be a bug in your graphics drivers ? Commented Apr 23 at 17:30
  • okay, what part exactly is not working ? i can see a rectangle that rotates ... what did you expect to happen ? Commented Apr 23 at 17:38
  • also for texture2 i think you mistyped fild1 for fild when drawing. Commented Apr 23 at 18:23
  • I am using Windows 11 and using Visual Studio to compile. Commented Apr 23 at 19:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.