bb6055aa0d2c119ef3fb910f3c9b668e48c27511
[dosdemo] / src / scr / cybersun.c
1 #include <stdio.h>
2 #include "demo.h"
3 #include "3dgfx.h"
4 #include "screen.h"
5 #include "gfxutil.h"
6 #include "mesh.h"
7 #include "image.h"
8
9 static int init(void);
10 static void destroy(void);
11 static void start(long trans_time);
12 static void draw(void);
13
14 static struct screen scr = {
15         "cybersun",
16         init,
17         destroy,
18         start,
19         0,
20         draw
21 };
22
23 static float cam_theta = 0, cam_phi = 10;
24 static float cam_dist = 6;
25
26 static struct g3d_mesh gmesh;
27 #define GMESH_GRIDSZ    20
28 #define GMESH_SIZE              50
29 static struct image gtex;
30
31 struct screen *cybersun_screen(void)
32 {
33         return &scr;
34 }
35
36 static int init(void)
37 {
38         int i, j;
39
40         if(gen_plane_mesh(&gmesh, GMESH_SIZE, GMESH_SIZE, GMESH_GRIDSZ, GMESH_GRIDSZ) == -1) {
41                 return -1;
42         }
43         for(i=0; i<gmesh.vcount; i++) {
44                 gmesh.varr[i].u *= GMESH_GRIDSZ;
45                 gmesh.varr[i].v *= GMESH_GRIDSZ;
46         }
47         if(load_image(&gtex, "data/pgrid.png") == -1) {
48                 return -1;
49         }
50
51         return 0;
52 }
53
54 static void destroy(void)
55 {
56         destroy_mesh(&gmesh);
57         destroy_image(&gtex);
58 }
59
60 static void start(long trans_time)
61 {
62         g3d_matrix_mode(G3D_PROJECTION);
63         g3d_load_identity();
64         g3d_perspective(50.0, 1.3333333, 0.5, 500.0);
65
66         g3d_enable(G3D_CULL_FACE);
67
68         g3d_clear_color(0, 0, 0);
69 }
70
71 static void draw(void)
72 {
73         int i;
74
75         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
76
77         g3d_matrix_mode(G3D_MODELVIEW);
78         g3d_load_identity();
79         g3d_translate(0, 0, -cam_dist);
80         g3d_rotate(cam_phi, 1, 0, 0);
81         g3d_rotate(cam_theta, 0, 1, 0);
82         if(opt.sball) {
83                 g3d_mult_matrix(sball_matrix);
84         }
85
86         g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
87
88         g3d_set_texture(gtex.width, gtex.height, gtex.pixels);
89         g3d_enable(G3D_TEXTURE_2D);
90
91         g3d_push_matrix();
92         g3d_rotate(-90, 1, 0, 0);
93         draw_mesh(&gmesh);
94         g3d_pop_matrix();
95
96         g3d_disable(G3D_TEXTURE_2D);
97
98         swap_buffers(fb_pixels);
99 }