Skip to content

Instantly share code, notes, and snippets.

@hertzsprung
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hertzsprung/6d80b1988bf7b50f8d03 to your computer and use it in GitHub Desktop.
Save hertzsprung/6d80b1988bf7b50f8d03 to your computer and use it in GitHub Desktop.
SDL2/cairo cross compiler test with mingw
#include <stdlib.h>
#include <stdio.h>
#include <cairo.h>
#include <SDL.h>
#include <SDL_render.h>
#include <SDL_video.h>
int main(int argc, char **argv) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initialize SDL: %s", SDL_GetError());
return EXIT_FAILURE;
}
atexit(SDL_Quit);
SDL_Window* window = SDL_CreateWindow("semaphore", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, 0);
if (window == NULL) {
fprintf(stderr, "Unable to create window: %s", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL) {
fprintf(stderr, "Unable to create renderer: %s", SDL_GetError());
return EXIT_FAILURE;
}
int width, height;
SDL_GetWindowSize(window, &width, &height);
SDL_Texture *texture;
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
width, height);
if (texture == NULL) {
fprintf(stderr, "Unable to create texture: %s", SDL_GetError());
return EXIT_FAILURE;
}
void *pixels;
int pitch;
if (SDL_LockTexture(texture, NULL, &pixels, &pitch) != 0) {
fprintf(stderr, "Failed to lock texture: %s", SDL_GetError());
return EXIT_FAILURE;
}
cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data(
pixels,
CAIRO_FORMAT_ARGB32,
width, height, pitch);
cairo_t *cr = cairo_create(cairo_surface);
char buf[128] = "hello world";
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_rectangle(cr, 0, 0, 128, 128);
cairo_fill(cr);
SDL_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(5000);
SDL_DestroyWindow(window);
return EXIT_SUCCESS;
}
PKG_CONFIG := pkg-config
#SDL_CONFIG := sdl2-config
SDL_CONFIG := /usr/x86_64-w64-mingw32/bin/sdl2-config
SDL_CFLAGS := $(shell $(SDL_CONFIG) --cflags)
SDL_LDFLAGS := $(shell $(SDL_CONFIG) --libs)
CAIRO_CFLAGS := $(shell $(PKG_CONFIG) cairo --cflags)
CAIRO_LDFLAGS := $(shell $(PKG_CONFIG) cairo --libs)
cairotest: cairotest.c
clang $(SDL_CFLAGS) $(SDL_LDFLAGS) $(CAIRO_CFLAGS) $(CAIRO_LDFLAGS) -o cairotest cairotest.c
cairotest.exe: cairotest.o
x86_64-w64-mingw32-gcc -o cairotest.exe cairotest.o $(SDL_LDFLAGS) $(CAIRO_LDFLAGS)
cairotest.o: cairotest.c
x86_64-w64-mingw32-gcc -o cairotest.o -c cairotest.c $(SDL_CFLAGS) $(CAIRO_CFLAGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment