annotate test/testintersections.c @ 5167:97423d858a1a

Added an intro to the features and goals of the 2D rendering API.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 03 Feb 2011 10:03:55 -0800
parents ad50b3db78bd
children d976b67150c5
rev   line source
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 /* Simple program: draw as many random objects on the screen as possible */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 #include <stdlib.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 #include <stdio.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 #include <time.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 #include "common.h"
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 #define NUM_OBJECTS 100
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 static CommonState *state;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 static int num_objects;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 static SDL_bool cycle_color;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 static SDL_bool cycle_alpha;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 static int cycle_direction = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 static int current_alpha = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 static int current_color = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 void
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
23 DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 int i;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 int x, y;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 int window_w, window_h;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 /* Query the sizes */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 SDL_GetWindowSize(window, &window_w, &window_h);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 for (i = 0; i < num_objects * 4; ++i) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 /* Cycle the color and alpha, if desired */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 if (cycle_color) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 current_color += cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 if (current_color < 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 current_color = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 if (current_color > 255) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 current_color = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 if (cycle_alpha) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 current_alpha += cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 if (current_alpha < 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 current_alpha = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 if (current_alpha > 255) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 current_alpha = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 }
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
56 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 (Uint8) current_color, (Uint8) current_alpha);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 x = rand() % window_w;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 y = rand() % window_h;
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
61 SDL_RenderDrawPoint(renderer, x, y);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 #define MAX_LINES 16
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 int num_lines = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 SDL_Rect lines[MAX_LINES];
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
68 static int
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
69 add_line(int x1, int y1, int x2, int y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
70 {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
71 if (num_lines >= MAX_LINES)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
72 return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
73 if ((x1 == x2) && (y1 == y2))
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
74 return 0;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 lines[num_lines].x = x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 lines[num_lines].y = y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 lines[num_lines].w = x2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 lines[num_lines].h = y2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 return ++num_lines;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 void
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
87 DrawLines(SDL_Window * window, SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 int i;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 int x1, y1, x2, y2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 int window_w, window_h;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 /* Query the sizes */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 SDL_GetWindowSize(window, &window_w, &window_h);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 for (i = 0; i < num_lines; ++i) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
97 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 if (i == -1) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
100 SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
101 SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
102 SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
103 SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 } else {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
105 SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 #define MAX_RECTS 16
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 int num_rects = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 SDL_Rect rects[MAX_RECTS];
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
113 static int
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
114 add_rect(int x1, int y1, int x2, int y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
115 {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
116 if (num_rects >= MAX_RECTS)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
117 return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
118 if ((x1 == x2) || (y1 == y2))
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
119 return 0;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
121 if (x1 > x2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
122 SWAP(int, x1, x2);
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
123 if (y1 > y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
124 SWAP(int, y1, y2);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
126 printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
127 x2 - x1, y2 - y1);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 rects[num_rects].x = x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 rects[num_rects].y = y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 rects[num_rects].w = x2 - x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 rects[num_rects].h = y2 - y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 return ++num_rects;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 static void
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
138 DrawRects(SDL_Window * window, SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 int i;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 int window_w, window_h;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 /* Query the sizes */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 SDL_GetWindowSize(window, &window_w, &window_h);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 for (i = 0; i < num_rects; ++i) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
147 SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
148 SDL_RenderFillRect(renderer, &rects[i]);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 static void
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
153 DrawRectLineIntersections(SDL_Window * window, SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 int i, j, window_w, window_h;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157 /* Query the sizes */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 SDL_GetWindowSize(window, &window_w, &window_h);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 for (i = 0; i < num_rects; i++)
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
161 for (j = 0; j < num_lines; j++) {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
162 int x1, y1, x2, y2;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
163 SDL_Rect r;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
165 r = rects[i];
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
166 x1 = lines[j].x;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
167 y1 = lines[j].y;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
168 x2 = lines[j].w;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
169 y2 = lines[j].h;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
170
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
171 if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
172 SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
173 SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
174 }
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 static void
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
179 DrawRectRectIntersections(SDL_Window * window, SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 int i, j;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
182
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 for (i = 0; i < num_rects; i++)
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
184 for (j = i + 1; j < num_rects; j++) {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
185 SDL_Rect r;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
186 if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
187 SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
188 SDL_RenderFillRect(renderer, &r);
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
189 }
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
192
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193 int
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 main(int argc, char *argv[])
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195 {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
196 int mouse_begin_x = -1, mouse_begin_y = -1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
197 int i, done;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 SDL_Event event;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199 Uint32 then, now, frames;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
200
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 /* Initialize parameters */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202 num_objects = NUM_OBJECTS;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204 /* Initialize test framework */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 state = CommonCreateState(argv, SDL_INIT_VIDEO);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 if (!state) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 return 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 for (i = 1; i < argc;) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 int consumed;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212 consumed = CommonArg(state, i);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 if (consumed == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214 consumed = -1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216 if (argv[i + 1]) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218 blendMode = SDL_BLENDMODE_NONE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 consumed = 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 blendMode = SDL_BLENDMODE_BLEND;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222 consumed = 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 blendMode = SDL_BLENDMODE_ADD;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 consumed = 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229 cycle_color = SDL_TRUE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 cycle_alpha = SDL_TRUE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 } else if (SDL_isdigit(*argv[i])) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 num_objects = SDL_atoi(argv[i]);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236 consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
237 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
238 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
239 if (consumed < 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
240 fprintf(stderr,
5143
e743b9c3f6d6 Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
241 "Usage: %s %s [--blend none|blend|add] [--cyclecolor] [--cyclealpha]\n",
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 argv[0], CommonUsage(state));
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 return 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245 i += consumed;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 if (!CommonInit(state)) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248 return 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 /* Create the windows and initialize the renderers */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252 for (i = 0; i < state->num_windows; ++i) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
253 SDL_Renderer *renderer = state->renderers[i];
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
254 SDL_SetRenderDrawBlendMode(renderer, blendMode);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
255 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
256 SDL_RenderClear(renderer);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
257 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
258
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 srand(time(NULL));
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261 /* Main render loop */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262 frames = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 then = SDL_GetTicks();
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 done = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265 while (!done) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 /* Check for events */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 ++frames;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268 while (SDL_PollEvent(&event)) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 CommonEvent(state, &event, &done);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 switch (event.type) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
271 case SDL_MOUSEBUTTONDOWN:
4465
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
272 mouse_begin_x = event.button.x;
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
273 mouse_begin_y = event.button.y;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 break;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275 case SDL_MOUSEBUTTONUP:
4465
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
276 if (event.button.button == 3)
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
277 add_line(mouse_begin_x, mouse_begin_y, event.button.x,
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
278 event.button.y);
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
279 if (event.button.button == 1)
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
280 add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
3e69e077cb95 Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
281 event.button.y);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
282 break;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
283 case SDL_KEYDOWN:
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
284 switch (event.key.keysym.sym) {
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
285 case 'l':
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
286 if (event.key.keysym.mod & KMOD_SHIFT)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
287 num_lines = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
288 else
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
289 add_line(rand() % 640, rand() % 480, rand() % 640,
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
290 rand() % 480);
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
291 break;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
292 case 'r':
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
293 if (event.key.keysym.mod & KMOD_SHIFT)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
294 num_rects = 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
295 else
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
296 add_rect(rand() % 640, rand() % 480, rand() % 640,
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
297 rand() % 480);
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
298 break;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
299 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 break;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301 default:
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
302 break;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
304 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305 for (i = 0; i < state->num_windows; ++i) {
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
306 SDL_Renderer *renderer = state->renderers[i];
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
307 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
308 SDL_RenderClear(renderer);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
310 DrawRects(state->windows[i], renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
311 DrawPoints(state->windows[i], renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
312 DrawRectRectIntersections(state->windows[i], renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
313 DrawLines(state->windows[i], renderer);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
314 DrawRectLineIntersections(state->windows[i], renderer);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315
5150
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5143
diff changeset
316 SDL_RenderPresent(renderer);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
317 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
319
3371
438ba87e9578 Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents: 2997
diff changeset
320 CommonQuit(state);
438ba87e9578 Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents: 2997
diff changeset
321
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
322 /* Print out some timing information */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
323 now = SDL_GetTicks();
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
324 if (now > then) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
325 double fps = ((double) frames * 1000) / (now - then);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
326 printf("%2.2f frames per second\n", fps);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
327 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
328 return 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
329 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
330
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
331 /* vi: set ts=4 sw=4 expandtab: */