Skip to content

Instantly share code, notes, and snippets.

@mutability
Created January 2, 2017 00:38
Show Gist options
  • Save mutability/82eea1c8f0b092cd7710a74922caa001 to your computer and use it in GitHub Desktop.
Save mutability/82eea1c8f0b092cd7710a74922caa001 to your computer and use it in GitHub Desktop.
// g++ -Wall -O -o sdlgpu-test $(pkg-config SDL2_gpu --cflags) $(sdl2-config --cflags) sdlgpu-test.cc $(pkg-config SDL2_gpu --libs) $(sdl2-config --libs)
#include <SDL_gpu.h>
int main(int argc, char* argv[])
{
GPU_SetDebugLevel(GPU_DEBUG_LEVEL_MAX);
GPU_Target* screen = GPU_Init(400, 300, GPU_DEFAULT_INIT_FLAGS);
if (!screen)
return 1;
SDL_Surface *surf = SDL_CreateRGBSurface(0, 16, 16, 32,
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 16;
rect.h = 16;
SDL_FillRect(surf, &rect, SDL_MapRGBA(surf->format, 64, 64, 64, 255));
rect.w = 4;
rect.h = 4;
rect.x = 4; rect.y = 4; SDL_FillRect(surf, &rect, SDL_MapRGBA(surf->format, 255, 0, 0, 255));
rect.x = 4; rect.y = 8; SDL_FillRect(surf, &rect, SDL_MapRGBA(surf->format, 0, 255, 0, 255));
rect.x = 8; rect.y = 4; SDL_FillRect(surf, &rect, SDL_MapRGBA(surf->format, 0, 0, 255, 255));
rect.x = 8; rect.y = 8; SDL_FillRect(surf, &rect, SDL_MapRGBA(surf->format, 255, 255, 0, 255));
GPU_Image *tex = GPU_CopyImageFromSurface(surf);
SDL_FreeSurface(surf);
GPU_SetAnchor(tex, 0, 0);
GPU_SetBlending(tex, GPU_FALSE);
bool done = false;
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = true;
else if (event.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == SDLK_ESCAPE)
done = true;
}
GPU_ClearRGBA(screen, 128, 128, 128, 255);
GPU_Rect src, dest;
// whole texture
src.x = 0;
src.y = 0;
src.w = tex->w;
src.h = tex->h;
dest.x = 10;
dest.y = 10;
dest.w = 256;
dest.h = 256;
GPU_SetImageFilter(tex, GPU_FILTER_NEAREST);
GPU_BlitRect(tex, &src, screen, &dest);
// red subtexture only:
// NEAREST
src.x = 4;
src.y = 4;
src.w = 4;
src.h = 4;
dest.x = 300;
dest.y = 10;
dest.w = 64;
dest.h = 64;
GPU_SetImageFilter(tex, GPU_FILTER_NEAREST);
GPU_BlitRect(tex, &src, screen, &dest);
// LINEAR
src.x = 4;
src.y = 4;
src.w = 4;
src.h = 4;
dest.x = 300;
dest.y = 84;
dest.w = 64;
dest.h = 64;
GPU_SetImageFilter(tex, GPU_FILTER_LINEAR);
GPU_BlitRect(tex, &src, screen, &dest);
// disable snap, fix up coordinates by hand
src.x = 4.5;
src.y = 4.5;
src.w = 3;
src.h = 3;
dest.x = 300;
dest.y = 158;
dest.w = 64;
dest.h = 64;
GPU_SetSnapMode(tex, GPU_SNAP_NONE);
GPU_SetImageFilter(tex, GPU_FILTER_LINEAR);
GPU_BlitRect(tex, &src, screen, &dest);
GPU_Flip(screen);
}
}
GPU_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment