Actual source code: ex13.c
1: static char help[] = "Tests the SDL3 PetscDraw implementation using an offscreen surface\n";
3: #include <petscsys.h>
4: #include <petscdraw.h>
5: #include <petsc/private/drawimpl.h>
7: // check that some pixel in a 3x3 neighborhood of the user coordinate has the given color
8: static PetscBool ProbeImage(PetscDraw draw, unsigned int w, unsigned int h, const unsigned char pixels[], PetscReal x, PetscReal y, int c)
9: {
10: int i, j;
12: PetscCallAbort(PETSC_COMM_SELF, PetscDrawCoordinateToPixel(draw, x, y, &i, &j));
13: for (int dj = -1; dj <= 1; dj++) {
14: for (int di = -1; di <= 1; di++) {
15: int ii = i + di, jj = j + dj;
17: if (ii >= 0 && ii < (int)w && jj >= 0 && jj < (int)h && pixels[jj * w + ii] == (unsigned char)c) return PETSC_TRUE;
18: }
19: }
20: return PETSC_FALSE;
21: }
23: int main(int argc, char **argv)
24: {
25: PetscDraw draw, sdraw;
26: unsigned char palette[PETSC_DRAW_MAXCOLOR][3];
27: unsigned char *pixels = NULL;
28: unsigned int w = 0, h = 0;
29: PetscBool match = PETSC_TRUE;
30: PetscReal tw, th;
31: PetscMPIInt rank, size;
33: PetscFunctionBeginUser;
34: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
35: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
36: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
37: // CI passes -nox, which would replace the offscreen SDL3 draw with PETSC_DRAW_NULL
38: PetscCall(PetscOptionsClearValue(NULL, "-nox"));
40: PetscCall(PetscDrawOpenSDL3(PETSC_COMM_WORLD, "Test SDL3", 0, 0, 240, 180, &draw));
41: PetscCall(PetscDrawSetFromOptions(draw));
42: // exercise viewports and clearing
43: PetscCall(PetscDrawSetViewPort(draw, .25, .25, .75, .75));
44: PetscCall(PetscDrawClear(draw));
45: PetscCall(PetscDrawSetViewPort(draw, 0, 0, 1, 1));
47: PetscCall(PetscDrawLine(draw, .1, .1, .9, .9, PETSC_DRAW_RED));
48: PetscCall(PetscDrawRectangle(draw, .1, .55, .3, .75, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE));
49: PetscCall(PetscDrawTriangle(draw, .6, .1, .85, .1, .725, .4, PETSC_DRAW_GREEN, PETSC_DRAW_GREEN, PETSC_DRAW_GREEN));
50: // exercise the color interpolated triangle
51: PetscCall(PetscDrawTriangle(draw, .55, .55, .95, .55, .75, .95, PETSC_DRAW_RED, PETSC_DRAW_GREEN, PETSC_DRAW_BLUE));
52: PetscCall(PetscDrawEllipse(draw, .2, .2, .15, .1, PETSC_DRAW_MAGENTA));
53: PetscCall(PetscDrawArrow(draw, .45, .5, .55, .5, PETSC_DRAW_ORANGE));
54: PetscCall(PetscDrawPoint(draw, .9, .6, PETSC_DRAW_CYAN));
55: // only the last process draws this. The combined image must contain it.
56: if (rank == size - 1) PetscCall(PetscDrawRectangle(draw, .88, .25, .95, .32, PETSC_DRAW_YELLOW, PETSC_DRAW_YELLOW, PETSC_DRAW_YELLOW, PETSC_DRAW_YELLOW));
57: // exercise the SDL3_ttf text rendering
58: PetscCall(PetscDrawStringSetSize(draw, .03, .05));
59: PetscCall(PetscDrawString(draw, .35, .85, PETSC_DRAW_BLACK, "SDL3 text"));
60: PetscCall(PetscDrawStringVertical(draw, .05, .3, PETSC_DRAW_BLACK, "Vert"));
61: // each process opens a font
62: PetscCall(PetscDrawStringGetSize(draw, &tw, &th));
63: match = (PetscBool)(tw > 0 && th > 0);
64: PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &match, 1, MPI_C_BOOL, MPI_LAND, PETSC_COMM_WORLD));
65: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "text size positive: %s\n", match == PETSC_TRUE ? "yes" : "no"));
66: // exercise text on a singleton, which borrows the font of draw, and text on draw after the singleton is destroyed
67: PetscCall(PetscDrawGetSingleton(draw, &sdraw));
68: PetscCall(PetscDrawString(sdraw, .35, .78, PETSC_DRAW_BLACK, "single"));
69: PetscCall(PetscDrawRestoreSingleton(draw, &sdraw));
70: PetscCall(PetscDrawString(draw, .6, .45, PETSC_DRAW_BLACK, "after"));
71: PetscCall(PetscDrawFlush(draw));
73: PetscUseTypeMethod(draw, getimage, palette, &w, &h, &pixels);
74: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Image size: %u x %u\n", w, h));
75: match = PETSC_TRUE;
76: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .3, .3, PETSC_DRAW_RED);
77: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "line probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
78: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .2, .65, PETSC_DRAW_BLUE);
79: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "rectangle probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
80: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .725, .15, PETSC_DRAW_GREEN);
81: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "triangle probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
82: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .2, .2, PETSC_DRAW_MAGENTA);
83: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "ellipse probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
84: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .5, .5, PETSC_DRAW_ORANGE);
85: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "arrow probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
86: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .9, .6, PETSC_DRAW_CYAN);
87: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "point probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
88: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .915, .285, PETSC_DRAW_YELLOW);
89: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "last process probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
90: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .5, .97, PETSC_DRAW_WHITE);
91: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "background probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
92: if (rank == 0) PetscCall(PetscFree(pixels));
94: // exercise resizing the offscreen surface
95: PetscCall(PetscDrawResizeWindow(draw, 300, 200));
96: PetscUseTypeMethod(draw, getimage, palette, &w, &h, &pixels);
97: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Resized image: %u x %u\n", w, h));
98: match = PETSC_TRUE;
99: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .5, .5, PETSC_DRAW_WHITE);
100: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "clean after resize: %s\n", match == PETSC_TRUE ? "yes" : "no"));
101: if (rank == 0) PetscCall(PetscFree(pixels));
103: // only the first process draws, so the image needs no reduction
104: if (rank == 0) PetscCall(PetscDrawRectangle(draw, .1, .1, .3, .3, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE));
105: PetscUseTypeMethod(draw, getimage, palette, &w, &h, &pixels);
106: match = PETSC_TRUE;
107: if (rank == 0) match = ProbeImage(draw, w, h, pixels, .2, .2, PETSC_DRAW_BLUE);
108: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "first process probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
109: if (rank == 0) PetscCall(PetscFree(pixels));
111: // the drawing of the last process is cleared, so again the image needs no reduction
112: if (rank == size - 1) PetscCall(PetscDrawRectangle(draw, .6, .6, .8, .8, PETSC_DRAW_RED, PETSC_DRAW_RED, PETSC_DRAW_RED, PETSC_DRAW_RED));
113: PetscCall(PetscDrawClear(draw));
114: if (rank == 0) PetscCall(PetscDrawRectangle(draw, .1, .1, .3, .3, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE, PETSC_DRAW_BLUE));
115: PetscUseTypeMethod(draw, getimage, palette, &w, &h, &pixels);
116: match = PETSC_TRUE;
117: if (rank == 0)
118: match = (PetscBool)(ProbeImage(draw, w, h, pixels, .2, .2, PETSC_DRAW_BLUE) == PETSC_TRUE && ProbeImage(draw, w, h, pixels, .7, .7, PETSC_DRAW_WHITE) == PETSC_TRUE && ProbeImage(draw, w, h, pixels, .7, .7, PETSC_DRAW_RED) == PETSC_FALSE);
119: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "cleared probe: %s\n", match == PETSC_TRUE ? "yes" : "no"));
120: if (rank == 0) PetscCall(PetscFree(pixels));
122: PetscCall(PetscDrawDestroy(&draw));
123: PetscCall(PetscFinalize());
124: return 0;
125: }
127: /*TEST
129: build:
130: requires: sdl3
132: test:
133: requires: sdl3
134: args: -draw_virtual
135: output_file: output/ex13.out
137: test:
138: suffix: 2
139: nsize: 2
140: requires: sdl3
141: args: -draw_virtual
142: output_file: output/ex13.out
144: test:
145: suffix: 3
146: requires: sdl3
147: args: -draw_virtual -draw_size .5
149: TEST*/