1 year ago
#84553
TANK1_41
How to load texture from png in sdl2 useing SDL_image.h
I am trying to load an image into a texture using SDL_image.h however whenever I try to run the line of code that uses IMG_Load("./assets/snow.png) it just crashes the program without any error messages other than exit code -1073741515. My CMake file and parts of my CPP file are below. I think I might be missing a dll but cannot find one that I don't already have that I would need.
#include <SDL.h>
#include <SDL_image.h>
SDL_Texture* playerTex;
void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen) {
int flags{0};
//checks if fullscreen is true
flags = fullscreen ? SDL_WINDOW_FULLSCREEN : 0;
//checks to make sure that the system is initialized correctly before making a window
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "subsystem running" << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
//check if window was made successfully
if (window) {
std::cout << "window created" << std::endl;
}
//create render
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Render created" << std::endl;
}
isRunning = true;
} else {
isRunning = false;
}
//TODO: why does this temp surface not set right prob issue with linking
SDL_Surface* tempSurface = IMG_Load("./assets/snow.png");
playerTex = SDL_CreateTextureFromSurface(renderer,tempSurface);
SDL_FreeSurface(tempSurface);
}
void Game::render() {
//clear render buffer
SDL_RenderClear(renderer);
//render texture
SDL_RenderCopy(renderer,playerTex,NULL,NULL);
//render new stuff
SDL_RenderPresent(renderer);
}
Here is my cmake file
cmake_minimum_required(VERSION 3.21)
project(learn_sdl2)
set(CMAKE_CXX_STANDARD 14)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/assets)
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/x64)
add_executable(learn_sdl2 main.cpp Game.cpp Game.h)
target_link_libraries(${PROJECT_NAME}
SDL2main
SDL2
SDL2_image
zlib1
libpng16-16
libjpeg-9)
c++
sdl-2
sdl-image
0 Answers
Your Answer