Create a gist now

Instantly share code, notes, and snippets.

( ノ╹◡◡╹)ノ SDL2_gfx を使ったデモっぽいやつ
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
#define ERROR_REPORT(_message_) \
fprintf( stderr, "%s:%d %s\n", __FILE__, __LINE__, _message_ )
int main(int argc, char ** argv) {
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
ERROR_REPORT( SDL_GetError() );
return -1;
}
SDL_Window *window = SDL_CreateWindow( "Hello, GFX",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL );
/* SDL_Surface *screen = SDL_GetWindowSurface( window ); */
SDL_Renderer *renderer = SDL_GetRenderer( window );
SDL_Color white = { 0xff, 0xff, 0xff, 0xff };
/* SDL_Color black = { 0x00, 0x00, 0x00, 0xff }; */
SDL_Point draw_points[] = {
{ 0, 5 },
{ 5, 5 },
{ 5, 11 },
{ 0, 11 }
};
/* SDL_FillRect( screen, NULL, SDL_MapRGB( screen->format, black.r, black.g, black.b ) ); */
for ( int i = 0; i < 4; ++ i ) {
int x1 = draw_points[i].x * (SCREEN_WIDTH / 16);
int y1 = draw_points[i].y * (SCREEN_HEIGHT / 16);
int x2 = draw_points[(i + 1) % 4].x * (SCREEN_WIDTH / 16);
int y2 = draw_points[(i + 1) % 4].y * (SCREEN_HEIGHT / 16);
printf( "(%3d, %3d) -> (%3d, %3d)\n", x1, y1, x2, y2 );
printf( "red: %d, green: %d, blue: %d, alpha: %d\n", white.r, white.g, white.b, white.a );
lineRGBA( renderer,
x1, y1,
x2, y2,
white.r, white.g, white.b, white.a );
}
SDL_RenderPresent( renderer );
int be_exit = 1;
SDL_Event an_event;
do {
if ( SDL_PollEvent( &an_event ) ) {
switch ( an_event.type ) {
case SDL_KEYDOWN:
be_exit = 0;
break;
case SDL_QUIT:
be_exit = 0;
break;
default:
break;
}
}
SDL_Delay( 1 );
} while ( be_exit );
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
SDL2_INCLUDES = $(shell pkg-config --cflags-only-I sdl2)
SDL2_DEFINES = $(shell pkg-config --cflags-only-other sdl2)
SDL2_LIBS = $(shell pkg-config --libs sdl2)
SDL2GFX_LIBS = $(shell pkg-config --libs SDL2_gfx)
CFLAGS = -W -Wall -O2 $(DEFINES) $(INCLUDES)
INCLUDES = $(SDL2_INCLUDES)
DEFINES = $(SDL2_DEFINES)
LIBS = $(SDL2GFX_LIBS) $(SDL2_LIBS)
target = demo-sdl2-gfx
.PHONY : all
all: $(target)
$(target): main.o
gcc $(LIBS) $^ -o $@
main.o: main.c
gcc $(CFLAGS) -c $^ -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment