2023-12-18 17:06:11 +01:00
|
|
|
#include "game_scene.h"
|
|
|
|
|
2023-12-31 15:04:17 +01:00
|
|
|
#include "render_core/application.h"
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-31 15:04:17 +01:00
|
|
|
#include "core/memory.h"
|
2023-12-31 19:57:59 +01:00
|
|
|
#include "render_core/3rd_glad.h"
|
2024-01-03 19:15:35 +01:00
|
|
|
#include "render_core/keyboard.h"
|
2023-12-31 15:04:17 +01:00
|
|
|
#include "render_core/mesh_utils.h"
|
2024-01-04 21:33:41 +01:00
|
|
|
#include "render_immediate/renderer.h"
|
2024-01-04 11:00:48 +01:00
|
|
|
//#include "render_core/font.h"
|
2023-12-19 15:23:49 +01:00
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
void GameScene::input_event(const Ref<InputEvent> &event) {
|
|
|
|
//ERR_PRINT(event->as_text());
|
2023-12-18 17:18:54 +01:00
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
Ref<InputEventKey> k = event;
|
2023-12-18 17:18:54 +01:00
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
if (k.is_valid()) {
|
|
|
|
if (k->is_echo()) {
|
|
|
|
return;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
uint32_t scancode = k->get_scancode();
|
|
|
|
bool pressed = k->is_pressed();
|
|
|
|
|
|
|
|
if (scancode == KEY_W) {
|
|
|
|
up = pressed;
|
|
|
|
} else if (scancode == KEY_A) {
|
|
|
|
left = pressed;
|
|
|
|
} else if (scancode == KEY_S) {
|
|
|
|
down = pressed;
|
|
|
|
} else if (scancode == KEY_D) {
|
|
|
|
right = pressed;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
|
|
|
if (mm->get_button_mask() & BUTTON_MASK_LEFT) {
|
|
|
|
tile_map->transform.translate(mm->get_relative());
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
}
|
2023-12-18 17:18:54 +01:00
|
|
|
|
2023-12-18 17:06:11 +01:00
|
|
|
void GameScene::update(float delta) {
|
2024-01-03 18:39:58 +01:00
|
|
|
Vector2 trn;
|
|
|
|
|
2023-12-18 17:18:54 +01:00
|
|
|
if (up) {
|
2024-01-03 18:39:58 +01:00
|
|
|
trn.y -= delta * 30.0;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-18 17:18:54 +01:00
|
|
|
if (down) {
|
2024-01-03 18:39:58 +01:00
|
|
|
trn.y += delta * 30.0;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-18 17:18:54 +01:00
|
|
|
if (left) {
|
2024-01-03 18:39:58 +01:00
|
|
|
trn.x -= delta * 30.0;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-18 17:18:54 +01:00
|
|
|
if (right) {
|
2024-01-03 18:39:58 +01:00
|
|
|
trn.x += delta * 30.0;
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2024-01-03 18:39:58 +01:00
|
|
|
if (up || down || left || right) {
|
|
|
|
sprite->transform.translate(trn);
|
2023-12-18 17:18:54 +01:00
|
|
|
}
|
2023-12-18 17:06:11 +01:00
|
|
|
}
|
2023-12-18 17:18:54 +01:00
|
|
|
|
2023-12-18 17:06:11 +01:00
|
|
|
void GameScene::render() {
|
2024-01-04 21:33:41 +01:00
|
|
|
//render_obj();
|
|
|
|
render_immediate();
|
|
|
|
}
|
|
|
|
void GameScene::render_immediate() {
|
|
|
|
Renderer *r = Renderer::get_singleton();
|
|
|
|
|
|
|
|
//r->camera_2d_reset();
|
|
|
|
|
|
|
|
r->clear_screen(Color());
|
|
|
|
|
|
|
|
r->draw_point(Vector2(15, 15));
|
|
|
|
r->draw_point(Vector2(18, 18), Color(1, 1, 0));
|
|
|
|
|
|
|
|
r->draw_line(Vector2(20, 20), Vector2(80, 20));
|
|
|
|
r->draw_line(Vector2(20, 20), Vector2(80, 40));
|
|
|
|
r->draw_line(Vector2(20, 30), Vector2(80, 30), Color(1, 1, 0));
|
|
|
|
r->draw_line(Vector2(20, 40), Vector2(80, 40), Color(1, 1, 0), 4);
|
|
|
|
r->draw_line(Vector2(20, 40), Vector2(80, 60), Color(1, 1, 0), 4);
|
|
|
|
|
|
|
|
r->draw_line_rect(Rect2(100, 100, 40, 40));
|
|
|
|
r->draw_line_rect(Rect2(150, 100, 40, 40), Color(1, 1, 0));
|
|
|
|
r->draw_line_rect(Rect2(200, 100, 40, 40), Color(1, 1, 1), 4);
|
|
|
|
|
|
|
|
r->draw_rect(Rect2(100, 150, 40, 40));
|
|
|
|
r->draw_rect(Rect2(150, 150, 40, 40), Color(1, 1, 0));
|
|
|
|
|
|
|
|
r->draw_texture(texture, Rect2(100, 200, 40, 40));
|
|
|
|
r->draw_texture(texture, Rect2(150, 200, 40, 40), Color(1, 0, 0));
|
|
|
|
|
|
|
|
r->draw_texture_clipped(texture, Rect2(20, 20, 30, 30), Rect2(100, 250, 40, 40));
|
|
|
|
r->draw_texture_clipped(texture, Rect2(20, 20, 30, 30), Rect2(150, 250, 40, 40), Color(1, 0, 0));
|
|
|
|
|
|
|
|
Transform2D t = Transform2D().rotated(Math_PI / 26.0);
|
|
|
|
|
|
|
|
r->draw_texture_tr(t, texture, Rect2(100, 300, 40, 40));
|
|
|
|
r->draw_texture_tr(t, texture, Rect2(150, 300, 40, 40), Color(1, 0, 0));
|
|
|
|
|
|
|
|
r->draw_texture_clipped_tr(t, texture, Rect2(20, 20, 30, 30), Rect2(100, 350, 40, 40));
|
|
|
|
r->draw_texture_clipped_tr(t, texture, Rect2(20, 20, 30, 30), Rect2(150, 350, 40, 40), Color(1, 0, 0));
|
|
|
|
|
|
|
|
//r->draw_mesh_2d(sprite->mesh_instance->mesh, texture, Vector2(1000, 500));
|
|
|
|
//r->draw_mesh_2d_tr(sprite->mesh_instance->mesh, texture, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 500)));
|
|
|
|
//r->draw_mesh_2d_mat(sprite->mesh_instance->mesh, material, Vector2(1000, 500));
|
|
|
|
//r->draw_mesh_2d_mat_tr(sprite->mesh_instance->mesh, material, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 600)));
|
|
|
|
|
|
|
|
r->draw_text_2d("draw_text_2d1", _font, Vector2(700, 100));
|
|
|
|
r->draw_text_2d("draw_text_2d2", _font, Vector2(800, 100), Color(1, 1, 0));
|
|
|
|
|
|
|
|
r->draw_text_2d_tf("draw_text_2d_tf1", _font, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 500)));
|
|
|
|
r->draw_text_2d_tf("draw_text_2d_tf2", _font, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1200, 500)), Color(0, 1, 0));
|
|
|
|
|
|
|
|
r->draw_text_2d_tf_material("draw_text_2d_tf_material1", _font, _font_test_mat, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 800)));
|
|
|
|
r->draw_text_2d_tf_material("draw_text_2d_tf_material2", _font, _font_test_mat, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1200, 800)), Color(1, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameScene::render_obj() {
|
2023-12-19 15:57:36 +01:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
2023-12-18 17:18:54 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2023-12-20 09:47:21 +01:00
|
|
|
static float rot = 0;
|
2023-12-31 20:38:51 +01:00
|
|
|
Transform t = camera->get_camera_transform();
|
|
|
|
t.basis = Basis(Vector3(0, 1, 0), rot);
|
|
|
|
camera->set_camera_transform(t);
|
2023-12-20 09:47:21 +01:00
|
|
|
rot += 0.01;
|
|
|
|
|
2023-12-31 10:48:11 +01:00
|
|
|
Ref<Image> d = texture->get_data();
|
|
|
|
texture->create_from_image(d);
|
|
|
|
|
2023-12-19 18:54:54 +01:00
|
|
|
camera->bind();
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-20 09:54:01 +01:00
|
|
|
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();
|
2024-01-04 12:12:57 +01:00
|
|
|
_font_test_sprite->render();
|
2024-01-04 14:37:11 +01:00
|
|
|
_font_test_mi->render();
|
|
|
|
|
2024-01-04 17:54:40 +01:00
|
|
|
_text_2d->render();
|
|
|
|
|
2024-01-04 11:00:48 +01:00
|
|
|
//TextRenderer::get_singleton()->font_init();
|
2024-01-04 01:31:59 +01:00
|
|
|
//TextRenderer::get_singleton()->font_print("test");
|
2023-12-18 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GameScene::GameScene() {
|
2023-12-18 17:18:54 +01:00
|
|
|
left = false;
|
2023-12-19 15:57:36 +01:00
|
|
|
right = false;
|
|
|
|
up = false;
|
|
|
|
down = false;
|
2023-12-18 17:06:11 +01:00
|
|
|
|
2023-12-19 21:42:52 +01:00
|
|
|
//int w;
|
|
|
|
//int h;
|
|
|
|
//SDL_GetWindowSize(Application::get_singleton()->window, &w, &h);
|
2023-12-18 17:18:54 +01:00
|
|
|
|
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-18 17:18:54 +01:00
|
|
|
|
2024-01-04 12:12:57 +01:00
|
|
|
_font.instance();
|
|
|
|
_font->load_default(31.5);
|
|
|
|
|
|
|
|
_font_test_sprite = memnew(Sprite);
|
|
|
|
|
2024-01-04 21:33:41 +01:00
|
|
|
_font_test_mat.instance();
|
2024-01-04 17:54:40 +01:00
|
|
|
_font_test_mat->texture = _font->get_texture();
|
2024-01-04 12:12:57 +01:00
|
|
|
_font_test_sprite->mesh_instance->material = _font_test_mat;
|
|
|
|
_font_test_sprite->width = _font->get_atlas_width();
|
|
|
|
_font_test_sprite->height = _font->get_atlas_height();
|
|
|
|
_font_test_sprite->transform.set_origin(Vector2(1000, 100));
|
|
|
|
_font_test_sprite->update_mesh();
|
|
|
|
|
2024-01-04 14:37:11 +01:00
|
|
|
_font_test_mesh.instance();
|
2024-01-04 15:23:39 +01:00
|
|
|
_font_test_mesh->vertex_dimesions = 2;
|
2024-01-04 14:37:11 +01:00
|
|
|
|
|
|
|
_font_test_mi = memnew(MeshInstance2D());
|
|
|
|
_font_test_mi->material = _font_test_mat;
|
2024-01-04 21:33:41 +01:00
|
|
|
_font_test_mi->mesh = _font_test_mesh;
|
2024-01-04 15:23:39 +01:00
|
|
|
//_font_test_mi->transform.scale(Vector2(10, 10));
|
|
|
|
_font_test_mi->transform.set_origin(Vector2(1000, 400));
|
2024-01-04 14:37:11 +01:00
|
|
|
|
2024-01-04 15:56:01 +01:00
|
|
|
_font->generate_mesh("asdfgh\nasdfvb", _font_test_mesh, Color(1, 1, 0));
|
2024-01-04 14:37:11 +01:00
|
|
|
_font_test_mesh->upload();
|
|
|
|
|
2023-12-31 01:33:19 +01:00
|
|
|
image.instance();
|
|
|
|
image->load_from_file("icon.png");
|
2023-12-31 02:43:19 +01:00
|
|
|
//image->bumpmap_to_normalmap();
|
2023-12-31 01:33:19 +01:00
|
|
|
|
2024-01-04 21:33:41 +01:00
|
|
|
texture.instance();
|
2023-12-31 01:33:19 +01:00
|
|
|
//texture->load_image("icon.png");
|
2023-12-31 10:48:11 +01:00
|
|
|
texture->create_from_image(image);
|
2023-12-18 17:18:54 +01:00
|
|
|
//ha a textúrának van alpha csatornája:
|
|
|
|
//texture->load_image("download.bmp", GL_RGBA, GL_RGBA);
|
|
|
|
|
2024-01-04 21:33:41 +01:00
|
|
|
material.instance();
|
2023-12-20 10:41:16 +01:00
|
|
|
material->texture = texture;
|
2023-12-18 17:18:54 +01:00
|
|
|
|
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
|
|
|
|
2023-12-18 17:18:54 +01:00
|
|
|
tile_map = new TileMap();
|
2024-01-05 00:48:07 +01:00
|
|
|
tile_map->material = material;
|
2023-12-20 14:17:03 +01:00
|
|
|
tile_map->atlas_size_x = 2;
|
|
|
|
tile_map->atlas_size_y = 2;
|
2023-12-18 17:18:54 +01:00
|
|
|
|
|
|
|
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-31 20:38:51 +01:00
|
|
|
Transform t = camera->get_camera_transform();
|
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-31 20:38:51 +01:00
|
|
|
t.origin.z -= 2;
|
|
|
|
camera->set_camera_transform(t);
|
2024-01-03 18:39:58 +01:00
|
|
|
|
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
|
|
|
|
2024-01-05 14:14:30 +01:00
|
|
|
mesh = Ref<Mesh>(memnew(Mesh()));
|
2023-12-20 09:47:21 +01:00
|
|
|
//cmaterial = memnew(ColoredMaterial());
|
2023-12-19 22:33:04 +01:00
|
|
|
//cmaterial->color = glm::vec4(1, 1, 0, 1);
|
2024-01-04 21:33:41 +01:00
|
|
|
color_material.instance();
|
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());
|
2024-01-05 14:14:30 +01:00
|
|
|
mi->material = color_material;
|
2023-12-20 09:54:01 +01:00
|
|
|
mi->mesh = mesh;
|
|
|
|
|
2023-12-20 14:33:24 +01:00
|
|
|
mi2 = memnew(MeshInstance3D());
|
2024-01-05 14:14:30 +01:00
|
|
|
mi2->material = color_material;
|
2023-12-20 09:54:01 +01:00
|
|
|
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
|
|
|
*/
|
2024-01-04 17:54:40 +01:00
|
|
|
|
|
|
|
_text_2d = memnew(Text2D);
|
|
|
|
_text_2d->set_font(_font);
|
|
|
|
_text_2d->set_text("Test Text2D.\n Newline.");
|
|
|
|
_text_2d->set_text_color(Color(1, 1, 0));
|
|
|
|
_text_2d->update();
|
|
|
|
_text_2d->transform.set_origin(Vector2(1200, 250));
|
2024-01-04 21:33:41 +01:00
|
|
|
|
|
|
|
Renderer::initialize();
|
2023-12-18 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GameScene::~GameScene() {
|
2024-01-04 21:33:41 +01:00
|
|
|
Renderer::destroy();
|
2023-12-20 14:17:03 +01:00
|
|
|
|
2024-01-04 21:33:41 +01:00
|
|
|
memdelete(tile_map);
|
2023-12-19 21:42:52 +01:00
|
|
|
memdelete(camera);
|
2023-12-20 14:17:03 +01:00
|
|
|
memdelete(sprite);
|
2023-12-18 17:06:11 +01:00
|
|
|
}
|