Hello,
I’m beginning into programming and I tried to set up SDL2 on code::blocks but i always got this errors:
I followed several tutos and it always finishes like that…
Can you help me ?
Hello,
I’m beginning into programming and I tried to set up SDL2 on code::blocks but i always got this errors:
on MS Windows 10
download codeblocks
https://www.codeblocks.org/downloads/binaries/
I have
codeblocks-20.03mingw-nosetup.zip
download SDL2
https://www.libsdl.org/download-2.0.php
I have
SDL2-devel-2.0.14-mingw.tar.gz (MinGW 32/64-bit)
make a folder
C:\Projects\dependencies\SDL2\
unzip
SDL2-devel-2.0.14-mingw.tar.gz
and put this folder
x86_64-w64-mingw32
in
C:\Projects\dependencies\SDL2
so you have
C:\Projects\dependencies\SDL2\x86_64-w64-mingw32
open codeblocks and create a new project / console application
project title = sdl_demo
Folder to create project in = C:\Projects\sdl\
hit ok and finish
in codeblocks menu select
Project / Build Options
make sure the project name is selected (not Debug or Release)
select
Search Directories \ Compiler
Add
C:\Projects\dependencies\SDL2\x86_64-w64-mingw32\include\SDL2
OK
select
Search Directories \ Linker
Add
C:\Projects\dependencies\SDL2\x86_64-w64-mingw32\lib
OK
select
Linker Settings \ Link Libraries
Add
mingw32
SDL2main
SDL2
go to
D:\Projects\dependencies\SDL2\x86_64-w64-mingw32\bin
copy
SDL2.dll
put it in
D:\Projects\sdl\sdl_demo
so you have
D:\Projects\sdl\sdl_demo\SDL2.dll
now go to google image search and type
sample.bmp
hopefully you’ll find a bitmap you like
I found some here
–BMP Files
I downloaded dots.bmp
and saved it to
C:\Projects\sdl\sdl_demo
now in your project in codeblocks open main.cpp and enter the following code
and from the codeblocks menu choose Build / Build and run
#include <SDL.h>
int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Texture *texture;
SDL_Event event;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 3;
}
if (SDL_CreateWindowAndRenderer(320, 240, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
return 3;
}
surface = SDL_LoadBMP("dots.bmp");
if (!surface) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface from image: %s", SDL_GetError());
return 3;
}
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture from surface: %s", SDL_GetError());
return 3;
}
SDL_FreeSurface(surface);
while (1) {
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
break;
}
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}