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;
}