1

I am doing a game using SDL.

The problem is that instead of getting image I see a black screen without any errors. And the most interesting thing that I've made another test project to see if I'll get an image and this one works excellent. In test project I have only main.cpp which does the same (not sure as i am getting black screen in my main project) and not using headers and this stuff.

Here is my code:

main.cpp(game project)

#include <SDL2\SDL.h>
#include <SDL2\SDL_image.h>
#include <iostream>
#include <stdio.h>
#include "RenderWindow.hpp"

int main(int argc, char** argv){
    if (SDL_Init(SDL_INIT_VIDEO) > 0)
        std::cout << "SDL_init has crushed" << SDL_GetError() << std::endl; // ініціалізація сдл

    if (!(IMG_Init(IMG_INIT_PNG)))
        std::cout << "IMG_init has failed. Error: " << IMG_GetError() << std::endl;

    RenderWindow window("Game",640,480);

    SDL_Texture* grassTexture = window.loadTexture("images/ground_grass_1.png");

    bool gameRunning = true;

    SDL_Event event;

    while(gameRunning)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
                gameRunning = false;
        }

        window.clear();
        window.render(grassTexture);
        window.display();
    }


    window.cleanUp();
    IMG_Quit();
    SDL_Quit();

    return 0;
}

RenderWindow.hpp

#pragma once 
#include <SDL2\SDL.h>
#include <SDL2\SDL_image.h>

class RenderWindow
{
    public:
        RenderWindow(const char* p_title, int p_w, int p_h);
        SDL_Texture* loadTexture(const char* p_filePath);
        void cleanUp();
        void clear();
        void render(SDL_Texture* p_text);
        void display();
    private:
        SDL_Window* window; 
        SDL_Renderer* renderer; 
}; 

renderwindow.cpp

#include <SDL2\SDL.h>
#include <SDL2\SDL_image.h>
#include <iostream>

#include "RenderWindow.hpp"



RenderWindow::RenderWindow(const char* p_title, int p_w, int p_h)
    :window(NULL), renderer(NULL) // присвоювання нульових поінтеров
{
    window = SDL_CreateWindow(p_title,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,p_w,p_h,SDL_WINDOW_SHOWN); // ініціалізація вікна

    if(window == NULL)
        std::cout << "Window failed to init" << SDL_GetError() << std::endl; 

    renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED); // ініціалізація рендера

    if(renderer == NULL)
        std::cout << "Renderer failed to init" << SDL_GetError() << std::endl;
}

SDL_Texture* RenderWindow::loadTexture(const char* p_filePath)
{
    SDL_Texture* texture = NULL;
    texture = IMG_LoadTexture(renderer,p_filePath);

    if(texture = NULL)
        std::cout << "Failed to load texture. Error: " << SDL_GetError() << std::endl;

    return texture; 
}

void RenderWindow::cleanUp()
{
    SDL_DestroyWindow(window);
}

void RenderWindow::clear()
{
    SDL_RenderClear(renderer);
}

void RenderWindow::render(SDL_Texture* p_tex)
{
    SDL_RenderCopy(renderer,p_tex,NULL,NULL);
}

void RenderWindow::display()
{
    SDL_RenderPresent(renderer);
}

and main.cpp(test project)

#include <SDL2/SDL.h>  // include the SDL library
#include <SDL2/SDL_image.h>  // include the SDL_image library for loading image files
#include <iostream>


int main(int argc, char* argv[]) {
  // initialize SDL
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cout << "SDL initialization failed: " << SDL_GetError() << std::endl;
    return 1;
  }

  // create the window
  SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  if (window == NULL) {
    std::cout << "Failed to create window: " << SDL_GetError() << std::endl;
    return 1;
  }

  // create the renderer
  SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  if (renderer == NULL) {
    std::cout << "Failed to create renderer: " << SDL_GetError() << std::endl;
    return 1;
  }

  // load the image file    
  SDL_Texture* texture = IMG_LoadTexture(renderer, "ground_grass_1.png");
  if (texture == NULL) {
    std::cout << "Failed to load image: " << SDL_GetError() << std::endl;
    return 1;
  }

  // render the image
  SDL_RenderCopy(renderer, texture, NULL, NULL);
  SDL_RenderPresent(renderer);

  // wait for 5 seconds
  SDL_Delay(5000);

  // clean up
  SDL_DestroyTexture(texture);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
kurkurindd
  • 23
  • 4
  • 2
    `if(texture = NULL)`...Why the assignment? – genpfault Jan 05 '23 at 21:54
  • Probably not related at this point but don't use ```\``` in your `#include` paths. Use `/`. – Ted Lyngmo Jan 05 '23 at 21:59
  • The lack of error checking in your "real" `main.cpp` makes it harder for you to see where the problems occur. – Ted Lyngmo Jan 05 '23 at 22:03
  • @genpfault That was actually it. :-) Changing `if(texture = NULL)` to `if(texture == NULL)` made it work. Conclusion: Turn on more warnings when compiling. Most compilers will warn you about that assignment. – Ted Lyngmo Jan 05 '23 at 22:05
  • @genpfault thank you sir! absolutely my bad to write this and no to see it after checking 100 times. – kurkurindd Jan 05 '23 at 22:20
  • @kurkurindd What compiler are you using? If you are using MSVC, enable more warnings with `/W4`. In `g++` and `clang++` you can use `-Wall -Wextra` and you should catch these mistakes (that we all do). There's also an old-school way of avoiding it called _"Yoda conditions"_. Try `if(NULL = texture)` and it shouldn't compile in any compiler no matter what warning levels you've enabled. This way of assuring the correct checks is mostly abandoned though, because compilers are so good at catching these mistakes nowadays. – Ted Lyngmo Jan 05 '23 at 22:24
  • @TedLyngmo I would like to but I have one more problem that I can't get rid of. The one and only way I can run my program using Makefile because VS Code still giving me message "SDL2/SDL.h: No such file or directory". I was searching on the internet but nothing helped. Maybe I have to make a new quiestion.. – kurkurindd Jan 05 '23 at 22:27
  • That sounds like you haven't added the SDL2 installation directories to the include paths and library paths in VS Code - but I don't know VS Code so I can't help you. A new question may be in order after you've searched for those two things. I'm sure something will pop up if you do. – Ted Lyngmo Jan 05 '23 at 22:29

0 Answers0