master uint/magnolia / include / renderer / test_scene.h
  1#ifndef TEST_SCENE_H
  2#define TEST_SCENE_H
  3
  4#include "mgmath.h"
  5#include "util.h"
  6#include "client/camera.h"
  7#include "renderer/renderer.h"
  8#include "world/block.h"
  9#include "world/chunk.h"
 10
 11static MGTexture block_textures[BLOCK_LAST];
 12static Chunk     chunks[WORLD_WIDTH][WORLD_DEPTH];
 13static MGCamera  cam;
 14
 15void test_scene_create(void)
 16{
 17	i32 x;
 18	i32 z;
 19
 20	block_textures[BLOCK_BRICK] =
 21	    renderer_texture_load("assets/resourcepacks/default/blocks/brick.png");
 22	if (block_textures[BLOCK_BRICK] == MGTEXTURE_INVALID)
 23		mg_die(MG_ERR_TEXTURE_LOAD, "Failed to load brick texture");
 24
 25	block_textures[BLOCK_DIRT] =
 26	    renderer_texture_load("assets/resourcepacks/default/blocks/dirt.png");
 27	if (block_textures[BLOCK_DIRT] == MGTEXTURE_INVALID)
 28		mg_die(MG_ERR_TEXTURE_LOAD, "Failed to load dirt texture");
 29
 30	block_textures[BLOCK_STONE] =
 31	    renderer_texture_load("assets/resourcepacks/default/blocks/stone.png");
 32	if (block_textures[BLOCK_STONE] == MGTEXTURE_INVALID)
 33		mg_die(MG_ERR_TEXTURE_LOAD, "Failed to load stone texture");
 34
 35	for (z = 0; z < WORLD_DEPTH; z++) {
 36		for (x = 0; x < WORLD_WIDTH; x++) {
 37			chunk_create(&chunks[x][z]);
 38
 39			if (!chunk_mesh_create(&chunks[x][z]))
 40				mg_die(MG_ERR_MESH_CREATE, "Failed to create chunk mesh");
 41		}
 42	}
 43
 44	camera_init(&cam, MG_V3(8.0f, 38.0f, 24.0f));
 45	cam.yaw = MG_AngleDeg(-180.0f);
 46	cam.pitch = MG_AngleDeg(-25.0f);
 47}
 48
 49void test_scene_draw(MGContext* ctx)
 50{
 51	MGMat4    proj;
 52	MGMat4    view;
 53	MGMat4    mvp;
 54	MGMat4    model;
 55	i32       x;
 56	i32       z;
 57	i32       width;
 58	i32       height;
 59	float     aspect;
 60	BlockType block;
 61
 62	platform_get_drawable_size(ctx, &width, &height);
 63
 64	if (width <= 0 || height <= 0)
 65		return;
 66
 67	aspect = (float)width / (float)height;
 68
 69	proj = camera_get_proj(&cam, aspect);
 70	view = camera_get_view(&cam);
 71
 72	for (z = 0; z < WORLD_DEPTH; z++) {
 73		for (x = 0; x < WORLD_WIDTH; x++) {
 74			model = MG_Translate(MG_V3(x * CHUNK_WIDTH, 0.0f, z * CHUNK_DEPTH));
 75			mvp = MG_MulM4(proj, MG_MulM4(view, model));
 76
 77			for (block = BLOCK_BRICK; block < BLOCK_LAST; block++) {
 78				if (chunks[x][z].meshes[block] == MGMESH_INVALID)
 79					continue;
 80				renderer_mesh_draw(chunks[x][z].meshes[block],
 81				                   block_textures[block], &mvp);
 82			}
 83		}
 84	}
 85}
 86
 87void test_scene_destroy(void)
 88{
 89	i32 x;
 90	i32 z;
 91
 92	for (z = 0; z < WORLD_DEPTH; z++) {
 93		for (x = 0; x < WORLD_WIDTH; x++)
 94			chunk_mesh_destroy(&chunks[x][z]);
 95	}
 96
 97	renderer_texture_destroy(block_textures[BLOCK_BRICK]);
 98	renderer_texture_destroy(block_textures[BLOCK_DIRT]);
 99	renderer_texture_destroy(block_textures[BLOCK_STONE]);
100
101	block_textures[BLOCK_BRICK] = MGTEXTURE_INVALID;
102	block_textures[BLOCK_DIRT] = MGTEXTURE_INVALID;
103	block_textures[BLOCK_STONE] = MGTEXTURE_INVALID;
104}
105
106#endif /* TEST_SCENE_H */