-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
cmake_minimum_required(VERSION 3.17) | ||
project(L07CairoCircle) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_compile_definitions(THREAD_SAFE) | ||
|
||
include_directories( | ||
/usr/local/include/SDL2 | ||
/usr/local/include/cairo | ||
) | ||
|
||
link_directories( | ||
/usr/local/lib | ||
) | ||
|
||
link_libraries( | ||
SDL2 | ||
cairo | ||
) | ||
|
||
add_executable(L07CairoCircle main.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <SDL.h> // include SDL header | ||
#include <cairo.h> | ||
|
||
|
||
#define FONT_SIZE 50 | ||
|
||
void draw(cairo_surface_t *cairosurf) { | ||
cairo_t *cr = cairo_create(cairosurf); | ||
|
||
cairo_set_font_size(cr, FONT_SIZE); | ||
cairo_move_to(cr, 0, FONT_SIZE); | ||
cairo_show_text(cr, "Hello Cairo"); | ||
|
||
cairo_destroy(cr); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
SDL_Surface *screen; // even with SDL2, we can still bring ancient code back | ||
SDL_Window *window; | ||
|
||
SDL_Init(SDL_INIT_VIDEO); // init video | ||
|
||
// create the window like normal | ||
window = SDL_CreateWindow( | ||
"Draw on surface", | ||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | ||
550, 400, SDL_WINDOW_SHOWN | ||
); | ||
|
||
// but instead of creating a renderer, we can draw directly to the screen | ||
screen = SDL_GetWindowSurface(window); | ||
cairo_surface_t *cairosurf = cairo_image_surface_create_for_data( | ||
screen->pixels, CAIRO_FORMAT_RGB24, | ||
screen->w, screen->h, screen->pitch | ||
); | ||
|
||
while (1) { | ||
// fill background | ||
SDL_FillRect(screen, NULL, 0xffffffff); | ||
draw(cairosurf); | ||
SDL_UpdateWindowSurface(window); | ||
|
||
SDL_Event event; | ||
if (SDL_PollEvent(&event)) { | ||
if (event.type == SDL_QUIT) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
cairo_surface_destroy(cairosurf); | ||
SDL_DestroyWindow(window); | ||
SDL_Quit(); | ||
return 0; | ||
} |