Skip to content

Commit

Permalink
添加Cairo使用文字的例子
Browse files Browse the repository at this point in the history
  • Loading branch information
plter committed Oct 20, 2020
1 parent fbf6c72 commit 317f4de
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 1 addition & 2 deletions L07CairoCircle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ 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

// create the window like normal
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
Expand Down
22 changes: 22 additions & 0 deletions L07CairoText/CMakeLists.txt
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)
55 changes: 55 additions & 0 deletions L07CairoText/main.c
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;
}

0 comments on commit 317f4de

Please sign in to comment.