Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created May 22, 2024 05:47
Show Gist options
  • Save Lightnet/d31f0865ab9776eac91498e948894543 to your computer and use it in GitHub Desktop.
Save Lightnet/d31f0865ab9776eac91498e948894543 to your computer and use it in GitHub Desktop.
sdl2 window shape test.
// https://github.com/libsdl-org/SDL/issues/8791
//strip down version for testing image
// use sdl2 default image bmp test.
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include "SDL.h"
#include <SDL_image.h>
#include "SDL_shape.h"
#define SHAPED_WINDOW_X 150
#define SHAPED_WINDOW_Y 150
#define SHAPED_WINDOW_DIMENSION 640
#undef main
int main(int argc, char **argv)
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Color black = { 0, 0, 0, 0xff };
SDL_Color white = { 255, 255, 255, 0xff };
SDL_Event event;
int should_exit = 0;
int button_down;
SDL_Init( SDL_INIT_VIDEO );
window = SDL_CreateShapedWindow("SDL_Shape test",
SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
SHAPED_WINDOW_DIMENSION, SHAPED_WINDOW_DIMENSION,
0);
//window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SHAPED_WINDOW_X, SHAPED_WINDOW_Y, SDL_WINDOW_SHOWN );
SDL_Surface * shape = SDL_LoadBMP("content/p01_shape24.bmp");
SDL_WindowShapeParams wsp;
wsp.binarizationCutoff = 255;
wsp.colorKey = black;
SDL_WindowShapeMode swsm;
swsm.mode = ShapeModeColorKey;
swsm.parameters = wsp;
SDL_SetWindowShape(window, shape, &swsm);
//SDL_SetWindowShape(window, shape, NULL);
renderer = SDL_CreateRenderer(window, -1, 0);
should_exit = 0;
button_down = 0;
while (should_exit == 0) {
while (SDL_PollEvent(&event)) {
switch (event.type)
{
case SDL_QUIT:
should_exit = 1;
break;
case SDL_KEYUP:
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
should_exit = 1;
}
break;
}
}
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
//Destroy window
SDL_DestroyWindow( window );
/* Call SDL_VideoQuit() before quitting. */
SDL_VideoQuit();
printf("CLOSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment