Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Created May 17, 2019 18:10
Show Gist options
  • Save riicchhaarrd/6bffa43f1306abafb9672d9c504ebf03 to your computer and use it in GitHub Desktop.
Save riicchhaarrd/6bffa43f1306abafb9672d9c504ebf03 to your computer and use it in GitHub Desktop.
sdl2 example for use with https://github.com/riicchhaarrd/cidscropt
#pragma comment(lib, "msvcrt.dll")
#pragma comment(lib, "SDL2.dll")
#pragma comment(lib, "SDL2_image.dll")
#pragma comment(lib, "opengl32.dll")
typedef struct
{
int type;
char rest[54];
} SDL_Event;
typedef struct
{
int flags;
void *format;
} SDL_Surface;
typedef struct
{
int x;
int y;
int w;
int h;
} SDL_Rect;
logic(pos)
{
while(level.running == true)
{
pos->x = pos->x + 1;
//printf("pos x = %\n", pos->x);
wait 0.05;
}
}
update(dest)
{
while(level.running == true)
{
ev = new SDL_Event;
ev.type=0;
while(SDL_PollEvent(ev) != 0)
{
//printf("ev types = %\n",ev.type);
if(ev.type == 0x100) //sdl_quit
{
level.running=false;
}
}
SDL_RenderClear(level.renderer);
for(i=0;i<level.textures.size;i++)
{
at = new SDL_Rect;
memcpy(at,dest,dest.size);
at.y = i * 100;
SDL_RenderCopy(level.renderer,level.textures[i],0,at);
}
SDL_RenderPresent(level.renderer);
wait (1/20);
}
SDL_DestroyWindow(level.wnd);
SDL_Quit();
}
in_array(a, v)
{
for(i=0;i<a.size;i++)
{
if(a[i]==v)
return true;
}
return false;
}
main() {
files = listdir(".");
//valid = [".jpg", ".png", ".bmp", ".gif", ".jpeg"];
valid = [".bmp"];
imgs = [];
for(i=0;i<files.size;i++)
{
for(j = 0; j < valid.size; j++)
{
if(strpos(files[i],valid[j]) != -1)
{
printf("%\n", files[i]);
imgs[imgs.size] = files[i];
}
}
}
i = SDL_Init(0x20); //0x20 means init video
width = 512;
height = 512;
level.wnd = SDL_CreateWindow("test", 100,100, width, height, 32 | 4); //2 = gl, 32 = resizable, 4 = shown
level.renderer = SDL_CreateRenderer(level.wnd,-1,0);
surf = SDL_GetWindowSurface(level.wnd);
copy = new SDL_Surface;
memcpy(copy, surf, copy.size);
//surface = IMG_Load("chiken.jpg");
textures = [];
for(i=0;i<imgs.size;i++)
{
surface = IMG_Load(imgs[i]);
printf("surface = %, %\n", surface, imgs[i]);
textures[textures.size] = SDL_CreateTextureFromSurface(level.renderer, surface);
SDL_FreeSurface(surface);
}
level.textures=textures;
red=255;
green=255;
blue=255;
//SDL_FillRect(surf, 0, SDL_MapRGB( copy.format, 255, 255, 255 ));
dest = new SDL_Rect;
dest.x=100;
dest.y=100;
dest.w=100;
dest.h=100;
thread logic(dest);
//SDL_UpdateWindowSurface(level.wnd);
level.running = true;
thread update(dest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment