1 | #include <nv/lib/sdl12.hh>
|
---|
2 | #include <nv/lib/sdl_image.hh>
|
---|
3 | #include <nv/interface/vertex_buffer.hh>
|
---|
4 | #include <nv/gl/gl_device.hh>
|
---|
5 | #include <nv/gl/image.hh>
|
---|
6 | #include <nv/interface/context.hh>
|
---|
7 | #include <nv/interface/window.hh>
|
---|
8 | #include <nv/interface/program.hh>
|
---|
9 | #include <nv/interface/texture2d.hh>
|
---|
10 | #include <nv/logging.hh>
|
---|
11 | #include <nv/logger.hh>
|
---|
12 | #include <glm/glm.hpp>
|
---|
13 | #include <glm/gtc/matrix_transform.hpp>
|
---|
14 | #include <glm/gtc/type_ptr.hpp>
|
---|
15 | #include <nv/string.hh>
|
---|
16 | #include <nv/types.hh>
|
---|
17 |
|
---|
18 | typedef glm::detail::tvec3<char> byte3;
|
---|
19 |
|
---|
20 | const nv::uint16 size_x = 16;
|
---|
21 | const nv::uint16 size_y = 16;
|
---|
22 | const nv::uint16 size_xy = size_x * size_y;
|
---|
23 |
|
---|
24 | nv::uint8 height[size_xy] =
|
---|
25 | {
|
---|
26 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
---|
27 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
---|
28 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
---|
29 | 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4,
|
---|
30 | 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4,
|
---|
31 | 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4,
|
---|
32 | 4, 4, 4, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
---|
33 | 4, 4, 4, 5, 5, 5, 4, 2, 2, 2, 2, 2, 2, 4, 4, 4,
|
---|
34 | 4, 4, 4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2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 2, 2, 2,
|
---|
56 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 2, 2, 2,
|
---|
57 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 2, 2, 2,
|
---|
58 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 2, 2, 2, 2, 2, 2,
|
---|
59 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 2, 2, 2, 2, 2, 2,
|
---|
60 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 2, 2, 2, 2, 2, 2,
|
---|
61 | 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 2, 2, 2, 2, 2, 2,
|
---|
62 | };
|
---|
63 |
|
---|
64 | struct world;
|
---|
65 | struct being;
|
---|
66 |
|
---|
67 | class application
|
---|
68 | {
|
---|
69 | public:
|
---|
70 | application();
|
---|
71 | bool initialize();
|
---|
72 | bool init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, int count, void* vertex, void* material );
|
---|
73 | bool run();
|
---|
74 | ~application();
|
---|
75 | protected:
|
---|
76 | nv::device* m_device;
|
---|
77 | nv::window* m_window;
|
---|
78 | nv::texture2d* m_texture;
|
---|
79 | nv::clear_state m_clear_state;
|
---|
80 | nv::render_state m_render_state;
|
---|
81 |
|
---|
82 | nv::program* m_char_program;
|
---|
83 | nv::program* m_box_program;
|
---|
84 | nv::vertex_array* m_char_va;
|
---|
85 | nv::vertex_array* m_box_va;
|
---|
86 | unsigned int m_count;
|
---|
87 | };
|
---|
88 |
|
---|
89 | application* g_application;
|
---|
90 |
|
---|
91 | application::application()
|
---|
92 | {
|
---|
93 | m_device = new nv::gl_device();
|
---|
94 | m_window = m_device->create_window( 800, 600 );
|
---|
95 |
|
---|
96 | nv::load_sdl_image_library();
|
---|
97 | SDL_Surface* texture = IMG_Load( "spritesheet.png" );
|
---|
98 | nv::image sprites( glm::ivec2( texture->w, texture->h ), 4, (nv::uint8*)texture->pixels );
|
---|
99 | nv::texture2d_sampler sampler( nv::texture2d_sampler::NEAREST, nv::texture2d_sampler::REPEAT );
|
---|
100 | m_texture = m_device->create_texture2d( sprites.get_size(), nv::texture2d::RGBA, nv::texture2d::UBYTE, sampler, (void*)sprites.get_data() );
|
---|
101 |
|
---|
102 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
103 | m_render_state.depth_test.enabled = true;
|
---|
104 | m_render_state.blending.enabled = true;
|
---|
105 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
106 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
107 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
108 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
109 | }
|
---|
110 |
|
---|
111 | bool application::initialize()
|
---|
112 | {
|
---|
113 | { // CHARACTER
|
---|
114 | byte3 vertex[6];
|
---|
115 | byte3 material[6];
|
---|
116 | int m = 16; int x = 0; int y = 0; int h = 0;
|
---|
117 | vertex[0] = byte3( x, h, y ); material[0] = byte3( m, 1, 0 );
|
---|
118 | vertex[1] = byte3( x, h, y+1 ); material[1] = byte3( m, 1, 0 );
|
---|
119 | vertex[2] = byte3( x+1, h, y+1 ); material[2] = byte3( m, 1, 0 );
|
---|
120 | vertex[3] = byte3( x+1, h, y+1 ); material[3] = byte3( m, 1, 0 );
|
---|
121 | vertex[4] = byte3( x+1, h, y ); material[4] = byte3( m, 1, 0 );
|
---|
122 | vertex[5] = byte3( x, h, y ); material[5] = byte3( m, 1, 0 );
|
---|
123 |
|
---|
124 | if (!init_program( "char", m_char_program, m_char_va, 6, vertex, material ) ) return false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | { // WORLD
|
---|
128 | byte3 vertex[size_x * size_y * 256];
|
---|
129 | byte3 material[size_x * size_y * 256];
|
---|
130 | m_count = 0;
|
---|
131 | int mcount = 0;
|
---|
132 | for (int i = 0; i < size_x * size_y; ++i )
|
---|
133 | {
|
---|
134 | int x = i % size_x;
|
---|
135 | int y = i / size_x;
|
---|
136 |
|
---|
137 | vertex[m_count++] = byte3( x, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
138 | vertex[m_count++] = byte3( x, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
139 | vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
140 |
|
---|
141 | vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
142 | vertex[m_count++] = byte3( x+1, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
143 | vertex[m_count++] = byte3( x, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
144 |
|
---|
145 | if (i > 0 && height[i-1] != height[i])
|
---|
146 | {
|
---|
147 | short dir = height[i-1] > height[i] ? -1 : 1;
|
---|
148 | nv::uint8 m = 1;
|
---|
149 | for ( int h = height[i-1]; h != height[i]; h = h + dir )
|
---|
150 | {
|
---|
151 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
152 | vertex[m_count++] = byte3( x, h, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
153 | vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
154 |
|
---|
155 | vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
156 | vertex[m_count++] = byte3( x, h+dir, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
157 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (i >= size_x && height[i-size_x] != height[i])
|
---|
162 | {
|
---|
163 | short dir = height[i-size_x] > height[i] ? -1 : 1;
|
---|
164 | nv::uint8 m = 1;
|
---|
165 | for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
|
---|
166 | {
|
---|
167 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
168 | vertex[m_count++] = byte3( x, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
169 | vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
170 |
|
---|
171 | vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
172 | vertex[m_count++] = byte3( x+1, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
173 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (!init_program( "box", m_box_program, m_box_va, m_count, vertex, material ) ) return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | return true;
|
---|
183 | }
|
---|
184 |
|
---|
185 | bool application::init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, int count, void* vertex, void* material )
|
---|
186 | {
|
---|
187 | p = m_device->create_program( nv::slurp( name+".vert" ), nv::slurp( name+".frag" ) );
|
---|
188 | va = m_device->create_vertex_array();
|
---|
189 |
|
---|
190 | nv::attribute* a;
|
---|
191 | a = p->get_attribute( "coords" ); if (a == nullptr) return false;
|
---|
192 | nv::vertex_buffer* vbcoords = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), vertex );
|
---|
193 | va->add_vertex_buffer( a->get_location(), vbcoords, nv::BYTE, 3 );
|
---|
194 |
|
---|
195 | a = p->get_attribute( "material" ); if (a == nullptr) return false;
|
---|
196 | nv::vertex_buffer* vbmaterial = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), material );
|
---|
197 | va->add_vertex_buffer( a->get_location(), vbmaterial, nv::BYTE, 3 );
|
---|
198 |
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | bool application::run()
|
---|
203 | {
|
---|
204 | int keypress = 0;
|
---|
205 |
|
---|
206 | glm::vec3 move( 0, 0, 0 );
|
---|
207 |
|
---|
208 | while(!keypress)
|
---|
209 | {
|
---|
210 | m_window->get_context()->clear( m_clear_state );
|
---|
211 |
|
---|
212 | // float d = -4.0f;
|
---|
213 | float angle = (float)(SDL_GetTicks() / 1000.0 * 45); // 45° per second
|
---|
214 | glm::vec3 axis_y(0.0, 1.0, 0.0);
|
---|
215 | glm::mat4 anim = glm::rotate(glm::mat4(1.0f), angle, axis_y);
|
---|
216 |
|
---|
217 | // glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, d));
|
---|
218 | // glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, d), glm::vec3(0.0, 1.0, 0.0));
|
---|
219 |
|
---|
220 | glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
|
---|
221 | glm::mat4 view = glm::lookAt(glm::vec3(0.0, 20.0, 5.0) + move, glm::vec3(0.0, 4.0, 0.0) + move, glm::vec3(0.0, 1.0, 0.0));
|
---|
222 | glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
|
---|
223 | //projection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 100.0f);
|
---|
224 | glm::mat4 mv = view * model;
|
---|
225 | glm::mat3 N = glm::transpose(glm::inverse(glm::mat3(mv)));
|
---|
226 |
|
---|
227 | m_texture->bind( 0 );
|
---|
228 | m_box_program->set_uniform( "matrix_mvp", projection * mv );
|
---|
229 | m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
|
---|
230 | m_box_program->set_uniform( "tex", 0 );
|
---|
231 | m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
|
---|
232 |
|
---|
233 | m_char_program->set_uniform( "matrix_mvp", projection * mv );
|
---|
234 | m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
|
---|
235 | m_char_program->set_uniform( "tex", 0 );
|
---|
236 | m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
|
---|
237 |
|
---|
238 | SDL_GL_SwapBuffers();
|
---|
239 | SDL_Event event;
|
---|
240 | while(SDL_PollEvent(&event))
|
---|
241 | {
|
---|
242 | switch (event.type)
|
---|
243 | {
|
---|
244 | case SDL_QUIT:
|
---|
245 | keypress = 1;
|
---|
246 | break;
|
---|
247 | case SDL_KEYDOWN:
|
---|
248 | switch (event.key.keysym.sym)
|
---|
249 | {
|
---|
250 | case SDLK_ESCAPE: keypress = 1; break;
|
---|
251 | case SDLK_LEFT: move.x = move.x - 1.0f; break;
|
---|
252 | case SDLK_RIGHT: move.x = move.x + 1.0f; break;
|
---|
253 | case SDLK_UP: move.z = move.z - 1.0f; break;
|
---|
254 | case SDLK_DOWN: move.z = move.z + 1.0f; break;
|
---|
255 | }
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | return true;
|
---|
261 | }
|
---|
262 |
|
---|
263 | application::~application()
|
---|
264 | {
|
---|
265 | delete m_char_program;
|
---|
266 | delete m_box_program;
|
---|
267 | delete m_char_va;
|
---|
268 | delete m_box_va;
|
---|
269 | delete m_texture;
|
---|
270 | delete m_window;
|
---|
271 | delete m_device;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /*
|
---|
275 | class test_resource : public nv::resource
|
---|
276 | {
|
---|
277 | public:
|
---|
278 | typedef std::shared_ptr< test_resource > ptr;
|
---|
279 | public:
|
---|
280 | test_resource() {}
|
---|
281 | virtual bool load() { return true; };
|
---|
282 | virtual bool unload() { return true; };
|
---|
283 | };
|
---|
284 | */
|
---|
285 | // nv::resource_manager::pointer()->register_resource_type<test_resource>("test");
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 | int main(int, char* [])
|
---|
290 | {
|
---|
291 | nv::logger log(nv::LOG_TRACE);
|
---|
292 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
293 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
294 |
|
---|
295 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
296 |
|
---|
297 | // nv::load_freetype_library();
|
---|
298 | //
|
---|
299 | // size_t depth = 3;
|
---|
300 | // nv::texture_atlas tex( glm::ivec2( 512, 512 ), depth );
|
---|
301 | // for ( nv::uint16 i = 10; i < 32; i += 2 )
|
---|
302 | // {
|
---|
303 | // nv::texture_font font( &tex, "aero.ttf", i );
|
---|
304 | // font.load_glyphs( "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
|
---|
305 | // }
|
---|
306 |
|
---|
307 | g_application = new application();
|
---|
308 |
|
---|
309 | // SDL_Surface* s = SDL_CreateRGBSurfaceFrom( (void*)tex.get_data(), 512, 512, depth * 8, 512 * depth,0,0,0,0);
|
---|
310 | // SDL_SaveBMP_RW( s, SDL_RWFromFile("test.bmp", "wb"), 1);
|
---|
311 |
|
---|
312 | if ( g_application->initialize() )
|
---|
313 | {
|
---|
314 | g_application->run();
|
---|
315 | }
|
---|
316 | delete g_application;
|
---|
317 |
|
---|
318 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
319 |
|
---|
320 | return 0;
|
---|
321 | }
|
---|
322 |
|
---|