[46] | 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,
|
---|
|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 2, 2, 6, 6, 6, 6, 6, 6, 2, 2, 2,
|
---|
| 54 | 3, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 2, 2, 2,
|
---|
| 55 | 2, 2, 2, 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::application()
|
---|
| 90 | {
|
---|
| 91 | m_device = new nv::gl_device();
|
---|
| 92 | m_window = m_device->create_window( 800, 600 );
|
---|
| 93 |
|
---|
| 94 | nv::load_sdl_image_library();
|
---|
| 95 | SDL_Surface* texture = IMG_Load( "spritesheet.png" );
|
---|
| 96 | nv::image sprites( glm::ivec2( texture->w, texture->h ), 4, (nv::uint8*)texture->pixels );
|
---|
| 97 | nv::texture2d_sampler sampler( nv::texture2d_sampler::NEAREST, nv::texture2d_sampler::REPEAT );
|
---|
| 98 | m_texture = m_device->create_texture2d( sprites.get_size(), nv::texture2d::RGBA, nv::texture2d::UBYTE, sampler, (void*)sprites.get_data() );
|
---|
| 99 |
|
---|
| 100 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
| 101 | m_render_state.depth_test.enabled = true;
|
---|
| 102 | m_render_state.blending.enabled = true;
|
---|
| 103 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
| 104 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 105 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
| 106 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | bool application::initialize()
|
---|
| 110 | {
|
---|
| 111 | { // CHARACTER
|
---|
| 112 | byte3 vertex[6];
|
---|
| 113 | byte3 material[6];
|
---|
| 114 | int m = 16; int x = 0; int y = 0; int h = 0;
|
---|
| 115 | vertex[0] = byte3( x, h, y ); material[0] = byte3( m, 1, 0 );
|
---|
| 116 | vertex[1] = byte3( x, h, y+1 ); material[1] = byte3( m, 1, 0 );
|
---|
| 117 | vertex[2] = byte3( x+1, h, y+1 ); material[2] = byte3( m, 1, 0 );
|
---|
| 118 | vertex[3] = byte3( x+1, h, y+1 ); material[3] = byte3( m, 1, 0 );
|
---|
| 119 | vertex[4] = byte3( x+1, h, y ); material[4] = byte3( m, 1, 0 );
|
---|
| 120 | vertex[5] = byte3( x, h, y ); material[5] = byte3( m, 1, 0 );
|
---|
| 121 |
|
---|
| 122 | if (!init_program( "char", m_char_program, m_char_va, 6, vertex, material ) ) return false;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | { // WORLD
|
---|
| 126 | byte3 vertex[size_x * size_y * 256];
|
---|
| 127 | byte3 material[size_x * size_y * 256];
|
---|
| 128 | m_count = 0;
|
---|
| 129 | int mcount = 0;
|
---|
| 130 | for (int i = 0; i < size_x * size_y; ++i )
|
---|
| 131 | {
|
---|
| 132 | int x = i % size_x;
|
---|
| 133 | int y = i / size_x;
|
---|
| 134 |
|
---|
| 135 | vertex[m_count++] = byte3( x, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 136 | vertex[m_count++] = byte3( x, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 137 | vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 138 |
|
---|
| 139 | vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 140 | vertex[m_count++] = byte3( x+1, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 141 | vertex[m_count++] = byte3( x, height[i], y ); material[mcount++] = byte3( map[i], 2, 0 );
|
---|
| 142 |
|
---|
| 143 | if (i > 0 && height[i-1] != height[i])
|
---|
| 144 | {
|
---|
| 145 | short dir = height[i-1] > height[i] ? -1 : 1;
|
---|
| 146 | nv::uint8 m = 1;
|
---|
| 147 | for ( int h = height[i-1]; h != height[i]; h = h + dir )
|
---|
| 148 | {
|
---|
| 149 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 150 | vertex[m_count++] = byte3( x, h, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 151 | vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 152 |
|
---|
| 153 | vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 154 | vertex[m_count++] = byte3( x, h+dir, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 155 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -dir, 0 );
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if (i >= size_x && height[i-size_x] != height[i])
|
---|
| 160 | {
|
---|
| 161 | short dir = height[i-size_x] > height[i] ? -1 : 1;
|
---|
| 162 | nv::uint8 m = 1;
|
---|
| 163 | for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
|
---|
| 164 | {
|
---|
| 165 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 166 | vertex[m_count++] = byte3( x, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 167 | vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 168 |
|
---|
| 169 | vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 170 | vertex[m_count++] = byte3( x+1, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 171 | vertex[m_count++] = byte3( x, h, y ); material[mcount++] = byte3( m, -3*dir, 0 );
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | if (!init_program( "box", m_box_program, m_box_va, m_count, vertex, material ) ) return false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | bool application::init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, int count, void* vertex, void* material )
|
---|
| 184 | {
|
---|
| 185 | p = m_device->create_program( nv::slurp( name+".vert" ), nv::slurp( name+".frag" ) );
|
---|
| 186 | va = m_device->create_vertex_array();
|
---|
| 187 |
|
---|
| 188 | nv::attribute* a;
|
---|
| 189 | a = p->get_attribute( "coords" ); if (a == nullptr) return false;
|
---|
| 190 | nv::vertex_buffer* vbcoords = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), vertex );
|
---|
| 191 | va->add_vertex_buffer( a->get_location(), vbcoords, nv::BYTE, 3 );
|
---|
| 192 |
|
---|
| 193 | a = p->get_attribute( "material" ); if (a == nullptr) return false;
|
---|
| 194 | nv::vertex_buffer* vbmaterial = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), material );
|
---|
| 195 | va->add_vertex_buffer( a->get_location(), vbmaterial, nv::BYTE, 3 );
|
---|
| 196 |
|
---|
| 197 | return true;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | bool application::run()
|
---|
| 201 | {
|
---|
| 202 | int keypress = 0;
|
---|
| 203 |
|
---|
| 204 | glm::vec3 move( 0, 0, 0 );
|
---|
| 205 |
|
---|
| 206 | while(!keypress)
|
---|
| 207 | {
|
---|
| 208 | m_window->get_context()->clear( m_clear_state );
|
---|
| 209 |
|
---|
| 210 | glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
|
---|
| 211 | 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));
|
---|
| 212 | glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
|
---|
| 213 | glm::mat4 mv = view * model;
|
---|
| 214 |
|
---|
| 215 | m_texture->bind( 0 );
|
---|
| 216 | m_box_program->set_uniform( "matrix_mvp", projection * mv );
|
---|
| 217 | m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
|
---|
| 218 | m_box_program->set_uniform( "tex", 0 );
|
---|
| 219 | m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
|
---|
| 220 |
|
---|
| 221 | m_char_program->set_uniform( "matrix_mvp", projection * mv );
|
---|
| 222 | m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
|
---|
| 223 | m_char_program->set_uniform( "tex", 0 );
|
---|
| 224 | m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
|
---|
| 225 |
|
---|
| 226 | SDL_GL_SwapBuffers();
|
---|
| 227 | SDL_Event event;
|
---|
| 228 | while(SDL_PollEvent(&event))
|
---|
| 229 | {
|
---|
| 230 | switch (event.type)
|
---|
| 231 | {
|
---|
| 232 | case SDL_QUIT:
|
---|
| 233 | keypress = 1;
|
---|
| 234 | break;
|
---|
| 235 | case SDL_KEYDOWN:
|
---|
| 236 | switch (event.key.keysym.sym)
|
---|
| 237 | {
|
---|
| 238 | case SDLK_ESCAPE: keypress = 1; break;
|
---|
| 239 | case SDLK_LEFT: move.x = move.x - 1.0f; break;
|
---|
| 240 | case SDLK_RIGHT: move.x = move.x + 1.0f; break;
|
---|
| 241 | case SDLK_UP: move.z = move.z - 1.0f; break;
|
---|
| 242 | case SDLK_DOWN: move.z = move.z + 1.0f; break;
|
---|
| 243 | }
|
---|
| 244 | break;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | return true;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | application::~application()
|
---|
| 252 | {
|
---|
| 253 | delete m_char_program;
|
---|
| 254 | delete m_box_program;
|
---|
| 255 | delete m_char_va;
|
---|
| 256 | delete m_box_va;
|
---|
| 257 | delete m_texture;
|
---|
| 258 | delete m_window;
|
---|
| 259 | delete m_device;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | int main(int, char* [])
|
---|
| 264 | {
|
---|
| 265 | nv::logger log(nv::LOG_TRACE);
|
---|
| 266 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 267 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 268 |
|
---|
| 269 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
[47] | 270 | application app;
|
---|
| 271 | if ( app.initialize() )
|
---|
[46] | 272 | {
|
---|
[47] | 273 | app.run();
|
---|
[46] | 274 | }
|
---|
| 275 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 276 |
|
---|
| 277 | return 0;
|
---|
| 278 | }
|
---|
| 279 |
|
---|