plotter_sdl.c
Go to the documentation of this file.
1 
13 //#define USE_GL_2 1
14 
15 #if defined(USE_GL_2)
16 #define GL_GLEXT_PROTOTYPES 1
17 #define GL3_PROTOTYPES 1
18 #endif
19 #include "plotter_sdl.h"
20 #include <SDL.h>
21 #include <SDL_opengl.h>
22 
23 
24 enum {
25  SCREEN_WIDTH = 640,
27 };
28 
29 
30 typedef struct
31 {
32  GLfloat x;
33  GLfloat y;
34 } vector_t;
35 
36 
37 static SDL_Surface *screen_ = NULL;
38 static vector_t *points_ = NULL;
39 static size_t max_points_size_ = 0;
40 static size_t points_size_ = 0;
41 static double draw_magnify_ = 0.1;
42 
43 
44 static void opengl_initialize(void)
45 {
46  int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
47 
48  // Initialize the display
49  int rgb_size[3];
50  switch (bpp) {
51  case 8:
52  rgb_size[0] = 3;
53  rgb_size[1] = 3;
54  rgb_size[2] = 2;
55  break;
56 
57  case 15:
58  case 16:
59  rgb_size[0] = 5;
60  rgb_size[1] = 5;
61  rgb_size[2] = 5;
62  break;
63 
64  default:
65  rgb_size[0] = 8;
66  rgb_size[1] = 8;
67  rgb_size[2] = 8;
68  break;
69  }
70  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
71  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
72  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
73  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
74  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
75  //SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
76 }
77 
78 
79 static void opengl_setup(void)
80 {
81  glPushAttrib(GL_ENABLE_BIT);
82  glDisable(GL_DEPTH_TEST);
83  glDisable(GL_CULL_FACE);
84  glEnable(GL_TEXTURE_2D);
85  glEnable(GL_BLEND);
86  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87 
88  glViewport(-1, -1, +1, +1);
89  glMatrixMode(GL_PROJECTION);
90  glLoadIdentity();
91 
92  glOrtho(-1.0, +1.0, -1.0, +1.0, -10.0, +10.0);
93 
94  glMatrixMode(GL_MODELVIEW);
95  glLoadIdentity();
96 
97  glEnable(GL_DEPTH_TEST);
98  glDepthFunc(GL_LESS);
99  glShadeModel(GL_SMOOTH);
100 }
101 
102 
103 static void draw_points(void)
104 {
105 
106 #if !defined(USE_GL_2)
107  size_t i;
108 
109  glBegin(GL_POINTS);
110  for (i = 0; i < points_size_; ++i) {
111  glVertex2i(points_[i].x, points_[i].y);
112  }
113  glEnd();
114 
115  points_size_ = 0;
116 
117 #else
118  int memory_size = points_size * sizeof(points[0]);
119 
120  glEnableClientState(GL_VERTEX_ARRAY);
121  glEnableClientState(GL_COLOR_ARRAY);
122 
123  glBindBuffer(GL_ARRAY_BUFFER, buffer_id);
124  glBufferData(GL_ARRAY_BUFFER, memory_size, points, GL_STATIC_DRAW);
125 
126  glInterleavedArrays(GL_V2F, 0, NULL);
127  glDrawArrays(GL_POINTS, 0, points_size);
128  glBindBuffer(GL_ARRAY_BUFFER, 0);
129 
130  glDisableClientState(GL_COLOR_ARRAY);
131  glDisableClientState(GL_VERTEX_ARRAY);
132 
133  points_size = 0;
134 #endif
135 }
136 
137 
138 static void enter2D(void)
139 {
140  glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
141 
142  glMatrixMode(GL_PROJECTION);
143  glPushMatrix();
144  glLoadIdentity();
145 
146  glOrtho(0.0, SCREEN_WIDTH - 1.0, 0.0, SCREEN_HEIGHT - 1.0, 0.0, 1.0);
147 
148  glMatrixMode(GL_MODELVIEW);
149  glPushMatrix();
150  glLoadIdentity();
151 
152  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
153 
154  glTranslatef(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0.0);
155 }
156 
157 
158 bool plotter_initialize(int data_size)
159 {
160  points_ = malloc(data_size * sizeof(vector_t));
161  if (!points_) {
162  return false;
163  }
164  max_points_size_ = data_size;
165 
166  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
167  printf("SDL_Init: %s\n", SDL_GetError());
168  return false;
169  }
170 
171  // 画面の作成
173  screen_ = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_OPENGL);
174  if (!screen_) {
175  return false;
176  }
177  opengl_setup();
178 
179  // 描画設定
180  glPointSize(2.0);
181 #if defined(USE_GL_2)
182  glGenBuffers(1, &buffer_id);
183 #endif
184  enter2D();
185 
186  // データの確保
187  // !!!
188 
189  return true;
190 }
191 
192 
194 {
195  SDL_Quit();
196 }
197 
198 
199 void plotter_clear(void)
200 {
201  glClearColor(0x00, 0x00, 0x00, 0xff);
202  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
203 }
204 
205 
206 void plotter_swap(void)
207 {
208  // 表示を入れ換えるときに、まだ描画していない内容を描画する
209  draw_points();
210 
211  SDL_GL_SwapBuffers();
212 }
213 
214 
215 void plotter_set_color(unsigned char r, unsigned g, unsigned b)
216 {
217  // 色を変更するときに、まとめて描画を行う
218  draw_points();
219 
220  glColor3f(r / 255.0, g / 255.0, b / 255.0);
221 }
222 
223 
224 void plotter_plot(float x, float y)
225 {
227  return;
228  }
229 
232  ++points_size_;
233 }
234 
235 
236 bool plotter_is_quit(void)
237 {
238  bool is_quit = false;
239  int magnify = 0;
240 
241  SDL_Event event;
242  while (SDL_PollEvent(&event)) {
243  switch (event.type) {
244 
245  case SDL_QUIT:
246  is_quit = true;
247  break;
248 
249  case SDL_KEYDOWN:
250  if ((event.key.keysym.sym == SDLK_q) ||
251  (event.key.keysym.sym == SDLK_F4)) {
252  is_quit = true;
253  }
254  if (event.key.keysym.sym == SDLK_COMMA) {
255  --magnify;
256  }
257  if (event.key.keysym.sym == SDLK_PERIOD) {
258  ++magnify;
259  }
260  break;
261 
262  case SDL_MOUSEBUTTONDOWN:
263  if (event.button.button == SDL_BUTTON_WHEELUP) {
264  --magnify;
265  } else if (event.button.button == SDL_BUTTON_WHEELDOWN) {
266  ++magnify;
267  }
268  break;
269  }
270  }
271 
272  // 描画の拡大率を変更する
273  while (magnify < 0) {
274  draw_magnify_ *= 0.90;
275  ++magnify;
276  }
277  while (magnify > 0) {
278  draw_magnify_ *= 1.10;
279  --magnify;
280  }
281  if (draw_magnify_ < 0.001) {
282  draw_magnify_ = 0.001;
283  } else if (draw_magnify_ > 10.0) {
284  draw_magnify_ = 10.0;
285  }
286 
287  return is_quit;
288 }
screen_
static SDL_Surface * screen_
Definition: plotter_sdl.c:37
plotter_plot
void plotter_plot(float x, float y)
Definition: plotter_sdl.c:224
SCREEN_HEIGHT
@ SCREEN_HEIGHT
Definition: plotter_sdl.c:26
SCREEN_WIDTH
@ SCREEN_WIDTH
Definition: plotter_sdl.c:25
draw_magnify_
static double draw_magnify_
Definition: plotter_sdl.c:41
vector_t
Definition: plotter_sdl.c:30
plotter_terminate
void plotter_terminate(void)
Definition: plotter_sdl.c:193
vector_t::y
GLfloat y
Definition: plotter_sdl.c:33
plotter_set_color
void plotter_set_color(unsigned char r, unsigned g, unsigned b)
Definition: plotter_sdl.c:215
points_size_
static size_t points_size_
Definition: plotter_sdl.c:40
plotter_is_quit
bool plotter_is_quit(void)
Definition: plotter_sdl.c:236
plotter_initialize
bool plotter_initialize(int data_size)
Definition: plotter_sdl.c:158
enter2D
static void enter2D(void)
Definition: plotter_sdl.c:138
draw_points
static void draw_points(void)
Definition: plotter_sdl.c:103
max_points_size_
static size_t max_points_size_
Definition: plotter_sdl.c:39
opengl_initialize
static void opengl_initialize(void)
Definition: plotter_sdl.c:44
plotter_sdl.h
Plotter (SDL)
points_
static vector_t * points_
Definition: plotter_sdl.c:38
plotter_swap
void plotter_swap(void)
Definition: plotter_sdl.c:206
plotter_clear
void plotter_clear(void)
Definition: plotter_sdl.c:199
opengl_setup
static void opengl_setup(void)
Definition: plotter_sdl.c:79
vector_t::x
GLfloat x
Definition: plotter_sdl.c:32


urg_c
Author(s): Satofumi Kamimura , Katsumi Kimoto, Adrian Boeing
autogenerated on Wed Mar 2 2022 01:08:11