File size: 8,506 Bytes
4559903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "Edit.h"

edit_c::edit_c(void) {
  // initialize the terrain
//  printf ("%6d: terrain\n", SDL_GetTicks ());
//  ter.resize(DEFAULT_SIDE_UNIV);
}


edit_c::~edit_c(void) {
}


void edit_c::setupOpenGl(void) {
  // set the clear color
  glClearColor(0.0f, 0.35f, .6f, 0.00f); // nice deep sky blue color
//  glClearColor (0.2f, 0.2f, 0.2f, 0.0f);
//  glClearColor (0.5f, 0.2f, 0.0f, 0.0f);  // orangey

  // set up the viewport
  glViewport(0, 0, gScreenW, gScreenH);

  // enable various things
  glEnable(GL_TEXTURE_2D);            // Enable 2D Texture Mapping
//  glBlendFunc(GL_SRC_ALPHA, GL_ONE);        // Select The Type Of Blending
  glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading
  glEnable(GL_DEPTH_TEST);
  glClearDepth(1.0f);
  glDepthFunc(GL_LEQUAL);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

  glEnable(GL_CULL_FACE);

  glEnable(GL_LIGHTING);

//  glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
//  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  glEnable(GL_COLOR_MATERIAL);

  // LIGHTING
  GLfloat LightAmbient[] =  { 0.5f, 0.2f, 0.0f, 1.0f };
  GLfloat LightDiffuse[] =  { 0.7f, 0.5f, 0.0f, 1.0f };
  GLfloat LightPosition[] =  { 0.0f, 100.0f, 0.0f, 1.0f };

  glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);    // Setup The Ambient Light
  glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);    // Setup The Diffuse Light
  glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);  // Position The Light
  glEnable(GL_LIGHT1);                // Enable Light One
//  glDisable (GL_LIGHT1);

  glDisable(GL_BLEND);

  // FOG
  GLuint fogMode[] = { GL_EXP, GL_EXP2, GL_LINEAR };  // Storage For Three Types Of Fog
  GLuint fogfilter = 2;                // Which Fog Mode To Use
//  GLfloat  fogColor[4] = {0.2f, 0.2f, 0.2f, 1.0f};    // Fog Color
  GLfloat  fogColor[4] = {0.5f, 0.2f, 0.0f, 1.0f};    // Fog Color

  glFogi(GL_FOG_MODE, fogMode[fogfilter]);      // Fog Mode
  glFogfv(GL_FOG_COLOR, fogColor);          // Set Fog Color
  glFogf(GL_FOG_DENSITY, 0.15f);            // How Dense Will The Fog Be
  glHint(GL_FOG_HINT, GL_DONT_CARE);          // Fog Hint Value
  glFogf(GL_FOG_START, 250.0f);            // Fog Start Depth
  glFogf(GL_FOG_END, 300.0f);            // Fog End Depth
  glDisable(GL_FOG);                  // Enables GL_FOG

  // go ahead and clear the buffer
  glClear(GL_COLOR_BUFFER_BIT);
  SDL_GL_SwapBuffers();
}


int edit_c::enter_edit_mode (void) {
  printf ("%6d: entered EDIT mode ----------------\n", SDL_GetTicks ());

  // change the mouse mode
  SDL_WM_GrabInput (SDL_GRAB_OFF);
  SDL_ShowCursor (1);

  int quit = 0;

  unsigned int ticks = SDL_GetTicks ();
  int frame = 0;

  int blocks_drawn = 0;

  setupOpenGl ();

  while (!quit) {
    unsigned int delta_t = SDL_GetTicks () - ticks;

    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // get the camera info
    GlCamera *cam = rts.gl_cam_setup ();

    // draw the octree
//    blocks_drawn += oct->draw (*cam);

    // draw the terrain as mesh
    ter.draw ();

    // draw the trees
//    forest.draw_block ();

    // swap the back buffer to the front
    SDL_GL_SwapBuffers ();

    // make sure everything goes through
    glFlush ();

    // do some frames per second calculating
    frame++;

    if (delta_t >= 5000) {
      printf ("FPS: %f, total frames: %d\n", (double)frame / ((double)delta_t / 1000.0), frame);
      printf ("average blocks per frame: %10.2f\n", (double)blocks_drawn / (double)frame);
      frame = 0;
      blocks_drawn = 0;
      ticks = SDL_GetTicks ();
    }

    // deal with the input
    quit = handle_input ();
  }

  printf ("%6d: exiting EDIT mode ----------------\n", SDL_GetTicks ());
  return 0;
}




// handle an SDL_Event
int edit_c::handle_input (void) {
  int quit = 0;
  mouse_moved = 0;

  // goes through all the queued events and deals with each one
  while (SDL_PollEvent (&sdlevent) && !quit) {
    switch (sdlevent.type) {
      case SDL_QUIT:
        quit = 1;
        break;

      case SDL_KEYDOWN:
        quit = handle_keystroke ();
        break;

      case SDL_KEYUP:
        handle_keyup ();
        break;

      case SDL_MOUSEMOTION:
        mouse_delta.x = -sdlevent.motion.xrel;
        mouse_delta.y = sdlevent.motion.yrel;

        mouse_position.x = sdlevent.motion.x;
        mouse_position.y = gScreenH - sdlevent.motion.y;

        mouse_moved = 1;

        break;

      // handle the mousebuttondown event
      case SDL_MOUSEBUTTONDOWN:
        handle_mouse_button_down (sdlevent.button.button, v2d_v (sdlevent.button.x, gScreenH - sdlevent.button.y));
        break;

      case SDL_MOUSEBUTTONUP:
        handle_mouse_button_up (sdlevent.button.button, v2d_v (sdlevent.button.x, gScreenH - sdlevent.button.y));
        break;

      default:
        break;
    }
  }


  // RTS MODE
  // handle mouse drag
  // FIXME: not here please!
  Uint8 ms;
  ms = SDL_GetMouseState (NULL, NULL);

  if (mouse_moved && (ms & SDL_BUTTON (SDL_BUTTON_LEFT))) {
    v2d_t md = v2d_scale (mouse_delta, 0.1);

    md.x = -md.x;

    rts.translate (md); }
  if (mouse_moved && (ms & SDL_BUTTON (SDL_BUTTON_RIGHT))) {
    v2d_t md = v2d_scale (mouse_delta, 0.002);

    md.x = -md.x;

    rts.pan (md); }

  return quit;
}


// deal with a keystroke
int edit_c::handle_keystroke (void) {
  Terrain t1 (8);
  t1.generateTilable (1.0, mPrng);
  t1.normalize (0.0, 10.0);

  Terrain t2 (64);
  t2.generateTilable (2.0, mPrng);
  t2.normalize (0.0, 4.0);

  switch (sdlevent.key.keysym.sym) {
    case SDLK_ESCAPE:  // quit
      return 1;

    case SDLK_t: // seed a new terrain
/*      for (int j = 0; j < DEFAULT_SIDE_UNIV; j++) {
        for (int i = 0; i < DEFAULT_SIDE_UNIV; ++i) {
          double u, v;

          u = 2.0 * static_cast<double>(i) * (8.0 / DEFAULT_SIDE_UNIV);
          v = 2.0 * static_cast<double>(j) * (8.0 / DEFAULT_SIDE_UNIV);

          double val1 = t1.getValueBilerp (u, v);

          u = 1.0 * static_cast<double>(i) * (64.0 / DEFAULT_SIDE_UNIV);
          v = 1.0 * static_cast<double>(j) * (64.0 / DEFAULT_SIDE_UNIV);

          double val2 = t2.getValueBilerp (u, v);

          ter.set_value (i, j, val1 + val2);

        }
      }

      ter.normalize (-20.0, 50.0);

      // convert the the terrain to an octree
      oct->from_terrain (ter);
*/
      break;

    case SDLK_o: // smooth the terrain
      ter.smooth ();

      // convert the the terrain to an octree
//      oct->from_terrain (ter);

      break;

    case SDLK_i: //alt_smooth the terrain
      ter.alt_smooth ();

      // convert the the terrain to an octree
//      oct->from_terrain (ter);

      break;

    case SDLK_p:    // pause
//      paused = (paused + 1) % 2;
      break;

    case SDLK_r:    // generate a random number
      rand ();
      break;

    default:
      break;
  }

  // don't quit!
  return 0;
}


// deal with a key up event
int edit_c::handle_keyup (void) {
/*  if (*mode == MODE_PLAYER) {
    switch (sdlevent.key.keysym.sym) {
      case SDLK_w:
        player->add_input_event (INPUT_TYPE_FORE_UP, 1.0);
        break;

      case SDLK_s:
        player->add_input_event (INPUT_TYPE_BACK_UP, 1.0);
        break;

      case SDLK_a:
        player->add_input_event (INPUT_TYPE_LEFT_UP, 1.0);
        break;

      case SDLK_d:
        player->add_input_event (INPUT_TYPE_RIGHT_UP, 1.0);
        break;

      case SDLK_SPACE:  // jump
        player->add_input_event (INPUT_TYPE_JUMP_UP, 1.0);
        break;

      default:
        break; } }*/

  return 0;
}


// deal with a mouse button down event
void edit_c::handle_mouse_button_down (int button, v2d_t pos) {
  switch (button) {
    case SDL_BUTTON_WHEELUP:
      // zoom in
      rts.zoom (-20.0);
      break;

    case SDL_BUTTON_WHEELDOWN:
      // zoom out
      rts.zoom (20.0);
      break;

    case SDL_BUTTON_RIGHT:
      break;

    default:
      break; }
}


// deal with a mouse button up event
void edit_c::handle_mouse_button_up (int button, v2d_t pos) {
/*  if (*mode == MODE_PLAYER) {
    switch (button) {
      case SDL_BUTTON_RIGHT:
        player->add_input_event (INPUT_TYPE_ROCKET_UP, 1.0);
        break;

      default:
        break;
    }
  }*/
}


// create a random world for a game session
void edit_c::create_world_for_game (void) {
  // generate the terrain
  ter.generateTilable (DEFAULT_TERRAIN_OFFSET, mPrng);

  // smooth the terrain
  ter.smooth ();

  ter.normalize (-25.0, 75.0);

  // create the octree
//  oct->from_terrain (ter);
}