From 317f4dec1c429ac1e2c24f0aae250e7a0f354bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=AE=87?= Date: Tue, 20 Oct 2020 11:06:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Cairo=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=87=E5=AD=97=E7=9A=84=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- L07CairoCircle/main.c | 3 +- L07CairoText/CMakeLists.txt | 22 +++++++++++++++ L07CairoText/main.c | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 L07CairoText/CMakeLists.txt create mode 100644 L07CairoText/main.c diff --git a/L07CairoCircle/main.c b/L07CairoCircle/main.c index 40c0461..0517ab4 100644 --- a/L07CairoCircle/main.c +++ b/L07CairoCircle/main.c @@ -16,7 +16,6 @@ void draw(cairo_surface_t *cairosurf) { int main(int argc, char *argv[]) { SDL_Surface *screen; // even with SDL2, we can still bring ancient code back SDL_Window *window; - SDL_Surface *image; SDL_Init(SDL_INIT_VIDEO); // init video @@ -24,7 +23,7 @@ int main(int argc, char *argv[]) { window = SDL_CreateWindow( "Draw on surface", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 550, 400, SDL_WINDOW_OPENGL + 550, 400, SDL_WINDOW_SHOWN ); // but instead of creating a renderer, we can draw directly to the screen diff --git a/L07CairoText/CMakeLists.txt b/L07CairoText/CMakeLists.txt new file mode 100644 index 0000000..5aea9f4 --- /dev/null +++ b/L07CairoText/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/L07CairoText/main.c b/L07CairoText/main.c new file mode 100644 index 0000000..5057de7 --- /dev/null +++ b/L07CairoText/main.c @@ -0,0 +1,55 @@ +#include // include SDL header +#include + + +#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; +}