-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhello-cairo.c
132 lines (104 loc) · 3.53 KB
/
hello-cairo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
the following file is
copied and modified from:
https://github.com/tsuu32/sdl2-cairo-example/blob/master/sdl2-cairo.c
*/
#include <SDL2/SDL.h>
#include <cairo/cairo.h>
#include <emscripten.h>
#include <stdbool.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window =
SDL_CreateWindow("Cairo in an SDL2 window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Renderer *renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
int window_width;
int window_height;
SDL_GetWindowSize(window, &window_width, &window_height);
printf(
"window_width=%d\n"
"window_height=%d\n",
window_width, window_height);
int renderer_width;
int renderer_height;
SDL_GetRendererOutputSize(renderer, &renderer_width, &renderer_height);
printf(
"renderer_width=%d\n"
"renderer_height=%d\n",
renderer_width, renderer_height);
int cairo_x_multiplier = renderer_width / window_width;
int cairo_y_multiplier = renderer_height / window_height;
SDL_Surface *sdl_surface =
SDL_CreateRGBSurface(0, renderer_width, renderer_height, 32, 0x00ff0000,
0x0000ff00, 0x000000ff, 0);
printf(
"sdl_surface->w=%d\n"
"sdl_surface->h=%d\n"
"sdl_surface->pitch=%d\n",
sdl_surface->w, sdl_surface->h, sdl_surface->pitch);
printf("sdl_surface->format->format=%s\n",
SDL_GetPixelFormatName(sdl_surface->format->format));
cairo_surface_t *cr_surface = cairo_image_surface_create_for_data(
(unsigned char *)sdl_surface->pixels, CAIRO_FORMAT_RGB24, sdl_surface->w,
sdl_surface->h, sdl_surface->pitch);
cairo_surface_set_device_scale(cr_surface, cairo_x_multiplier,
cairo_y_multiplier);
cairo_t *cr = cairo_create(cr_surface);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
// White background with SDL2 API
// SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format, 255, 255,
// 255));
// White background with cairo API
cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
cairo_rectangle(cr, 0, 0, 640, 480);
cairo_fill(cr);
double xc = 320.0;
double yc = 240.0;
double radius = 200.0;
double angle1 = 45.0 * (M_PI / 180.0);
double angle2 = 180.0 * (M_PI / 180.0);
cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
cairo_set_line_width(cr, 10.0);
cairo_arc(cr, xc, yc, radius, angle1, angle2);
cairo_stroke(cr);
cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.6);
cairo_set_line_width(cr, 6.0);
cairo_arc(cr, xc, yc, 10.0, 0, 2 * M_PI);
cairo_fill(cr);
cairo_arc(cr, xc, yc, radius, angle1, angle1);
cairo_line_to(cr, xc, yc);
cairo_arc(cr, xc, yc, radius, angle2, angle2);
cairo_line_to(cr, xc, yc);
cairo_stroke(cr);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, sdl_surface);
SDL_FreeSurface(sdl_surface);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
bool done = false;
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
// SDL_Delay(100);
emscripten_sleep(100);
}
cairo_destroy(cr);
cairo_surface_destroy(cr_surface);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}