sfw/game_scene.cpp

262 lines
5.2 KiB
C++
Raw Normal View History

#include "game_scene.h"
#include "application.h"
2023-12-19 15:23:49 +01:00
#include "3rd_glad.h"
2023-12-19 15:57:36 +01:00
#include "memory.h"
2023-12-20 09:47:21 +01:00
#include "mesh_utils.h"
2023-12-19 15:23:49 +01:00
2023-12-19 15:01:48 +01:00
void GameScene::event() {
/*
switch (ev.type) {
case SDL_WINDOWEVENT: {
switch (ev.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED: {
int width = ev.window.data1;
int height = ev.window.data2;
float ar = static_cast<float>(width) / static_cast<float>(height);
camera->width = camera->height * ar;
glViewport(0, 0, width, height);
break;
}
}
break;
}
case SDL_KEYDOWN: {
if (ev.key.keysym.scancode == SDL_SCANCODE_A) {
left = true;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_W) {
up = true;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_S) {
down = true;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_D) {
right = true;
}
break;
}
case SDL_KEYUP: {
if (ev.key.keysym.scancode == SDL_SCANCODE_A) {
left = false;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_W) {
up = false;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_S) {
down = false;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_D) {
right = false;
}
break;
}
}
2023-12-19 15:01:48 +01:00
*/
}
void GameScene::update(float delta) {
2023-12-19 15:01:48 +01:00
/*
if (up) {
sprite->position.y += delta * 3.0;
}
if (down) {
sprite->position.y -= delta * 3.0;
}
if (left) {
sprite->position.x -= delta * 3.0;
}
if (right) {
sprite->position.x += delta * 3.0;
}
if (sprite->position.x < 1.5) {
sprite->position.x = 1.5;
}
if (sprite->position.x > 14.5) {
sprite->position.x = 14.5;
}
if (sprite->position.y < 1.5) {
sprite->position.y = 1.5;
}
if (sprite->position.y > 14.5) {
sprite->position.y = 14.5;
}
2023-12-19 15:01:48 +01:00
*/
}
void GameScene::render() {
2023-12-19 15:57:36 +01:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
2023-12-20 09:47:21 +01:00
static float rot = 0;
camera->camera_transform.basis = Basis(Vector3(0, 1, 0), rot);
rot += 0.01;
2023-12-19 18:54:54 +01:00
camera->bind();
2023-12-20 14:17:03 +01:00
2023-12-19 15:23:49 +01:00
//sprite->render();
2023-12-19 21:42:52 +01:00
//material->bind();
2023-12-20 09:54:01 +01:00
//color_material->bind();
//mesh->render();
static float rotmi = 0;
mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
mi->render();
2023-12-19 21:42:52 +01:00
2023-12-20 10:41:16 +01:00
camera_2d->bind();
sprite->render();
2023-12-20 14:17:03 +01:00
tile_map->render();
}
GameScene::GameScene() {
2023-12-19 15:01:48 +01:00
/*
left = false;
2023-12-19 15:57:36 +01:00
right = false;
up = false;
down = false;
2023-12-19 21:42:52 +01:00
*/
2023-12-19 21:42:52 +01:00
//int w;
//int h;
//SDL_GetWindowSize(Application::get_singleton()->window, &w, &h);
2023-12-19 21:42:52 +01:00
//float ar = static_cast<float>(w) / static_cast<float>(h);
//camera->width = camera->height * ar;
2023-12-20 10:41:16 +01:00
texture = new Texture();
texture->load_image("icon.png");
//ha a textúrának van alpha csatornája:
//texture->load_image("download.bmp", GL_RGBA, GL_RGBA);
2023-12-20 10:41:16 +01:00
material = new TextureMaterial2D();
material->texture = texture;
2023-12-20 10:41:16 +01:00
sprite = new Sprite();
sprite->mesh_instance->material = material;
sprite->width = 500;
sprite->height = 500;
sprite->transform.set_origin(Vector2(250, 250));
2023-12-19 21:42:52 +01:00
//sprite->region_x = 7.0 * (1.0 / 16.0);
//sprite->region_y = 7.0 * (1.0 / 16.0);
//sprite->region_width = 1.0 / 16.0;
//sprite->region_height = 1.0 / 16.0;
2023-12-20 10:41:16 +01:00
sprite->update_mesh();
2023-12-20 14:17:03 +01:00
tile_map = new TileMap();
tile_map->material = material;
2023-12-20 14:17:03 +01:00
tile_map->atlas_size_x = 2;
tile_map->atlas_size_y = 2;
tile_map->allocate_data();
for (int x = 0; x < tile_map->size_x; ++x) {
for (int y = 0; y < tile_map->size_y; ++y) {
if (x == 0 || y == 0 || x == tile_map->size_x - 1 || y == tile_map->size_y - 1) {
tile_map->set_data(x, y, 2);
} else {
tile_map->set_data(x, y, 1);
}
}
}
tile_map->build_mesh();
2023-12-20 14:17:03 +01:00
tile_map->transform.scale(Vector2(32, 32));
tile_map->transform.set_origin(Vector2(500, 500));
2023-12-19 15:57:36 +01:00
2023-12-20 09:47:21 +01:00
camera = new PerspectiveCamera();
2023-12-19 22:33:04 +01:00
//camera->width = 2;
//camera->height = 2;
//camera->position.x = 0;
//camera->position.y = 0;
2023-12-19 18:54:54 +01:00
//camera->position.z = -2;
2023-12-20 09:47:21 +01:00
camera->camera_transform.origin.z -= 2;
2023-12-20 10:41:16 +01:00
camera->screen_aspect_ratio = 1920.0 / 1080.0;
camera_2d = memnew(Camera2D);
camera_2d->size = Vector2(1920, 1080);
2023-12-19 18:54:54 +01:00
2023-12-20 09:47:21 +01:00
mesh = memnew(Mesh());
//cmaterial = memnew(ColoredMaterial());
2023-12-19 22:33:04 +01:00
//cmaterial->color = glm::vec4(1, 1, 0, 1);
2023-12-20 09:47:21 +01:00
color_material = memnew(ColorMaterial());
2023-12-19 15:57:36 +01:00
2023-12-20 09:47:21 +01:00
//mesh->clear();
MeshUtils::create_cone(mesh);
mesh->upload();
2023-12-19 15:57:36 +01:00
2023-12-20 14:33:24 +01:00
mi = memnew(MeshInstance3D());
2023-12-20 09:54:01 +01:00
mi->material = color_material;
mi->mesh = mesh;
2023-12-20 14:33:24 +01:00
mi2 = memnew(MeshInstance3D());
2023-12-20 09:54:01 +01:00
mi2->material = color_material;
mi2->mesh = mesh;
mi2->transform.origin.x = 1;
mi->children.push_back(mi2);
2023-12-19 15:57:36 +01:00
//float width = 1;
//float height = 1;
//float region_x = 0;
//float region_y = 0;
//float region_width = 1;
//float region_height = 1;
//float w2 = width / 2.0;
//float h2 = height / 2.0;
//mesh->add_uv(region_x, region_y);
//mesh->add_vertex2(-w2, h2);
//mesh->add_uv(region_x + region_width, region_y + region_height);
//mesh->add_vertex2(w2, -h2);
//mesh->add_uv(region_x, region_y + region_height);
//mesh->add_vertex2(-w2, -h2);
//mesh->add_uv(region_x + region_width, region_y);
//mesh->add_vertex2(w2, h2);
2023-12-20 09:47:21 +01:00
/*
2023-12-19 15:57:36 +01:00
mesh->add_vertex2(0, 0.5);
mesh->add_vertex2(-0.5, -0.5);
mesh->add_vertex2(0.5, -0.5);
mesh->add_triangle(0, 1, 2);
//mesh->add_triangle(0, 1, 3);
mesh->upload();
2023-12-20 09:47:21 +01:00
*/
}
GameScene::~GameScene() {
2023-12-20 14:17:03 +01:00
memdelete(tile_map);
2023-12-19 21:42:52 +01:00
memdelete(camera);
2023-12-20 14:17:03 +01:00
memdelete(texture);
2023-12-19 21:42:52 +01:00
2023-12-20 14:17:03 +01:00
memdelete(sprite);
2023-12-19 15:57:36 +01:00
2023-12-19 18:54:54 +01:00
memdelete(camera);
2023-12-19 15:57:36 +01:00
memdelete(mesh);
2023-12-20 14:17:03 +01:00
memdelete(material);
2023-12-20 09:47:21 +01:00
memdelete(color_material);
}