Added more classes form other projects.

This commit is contained in:
Relintai 2023-12-18 17:18:54 +01:00
parent c33dda3715
commit e5561e888d
13 changed files with 529 additions and 104 deletions

68
color_material.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef COLOR_MATERIAL_H
#define COLOR_MATERIAL_H
#include "material.h"
#include "glm/vec4.hpp"
#include "camera.h"
#include "./glm/gtc/type_ptr.hpp"
class ColorMaterial : public Material {
public:
int get_material_id() {
return 2;
}
void bind_uniforms() {
glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix));
glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix));
}
void setup_uniforms() {
projection_matrix_location = get_uniform("u_proj_matrix");
model_view_matrix_location = get_uniform("u_model_view_matrix");
}
const GLchar** get_vertex_shader_source() {
static const GLchar *vertex_shader_source[] = {
"uniform mat4 u_proj_matrix;\n"
"uniform mat4 u_model_view_matrix;\n"
"\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_color;\n"
"\n"
"varying vec4 v_color;\n"
"\n"
"void main() {\n"
" v_color = a_color;\n"
" gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n"
"}\n"
};
return vertex_shader_source;
}
const GLchar** get_fragment_shader_source() {
static const GLchar *fragment_shader_source[] = {
"precision mediump float;"
"varying vec4 v_color;\n"
"\n"
"void main() { gl_FragColor = v_color; }\n"
};
return fragment_shader_source;
}
ColorMaterial() {
color = glm::vec4(1, 1, 1, 1);
}
GLint projection_matrix_location;
GLint model_view_matrix_location;
glm::vec4 color;
};
#endif

36
compile_android.sh Executable file
View File

@ -0,0 +1,36 @@
#ANDROID_HOME be kell legyen állítva
#export ANDROID_HOME=<path>
cd libs/SDL2-2.0.12-src
rm -Rf build
mkdir build
cd ..
cd ..
./libs/SDL2-2.0.12-src/build-scripts/androidbuild.sh net.tapp.sdl ./main.cpp ./shader.cpp ./material.cpp ./mesh.cpp ./mesh_instance.cpp ./mesh_utils.cpp \
./texture.cpp ./camera.cpp ./application.cpp ./scene.cpp ./game_scene.cpp ./object_2d.cpp ./tile_map.cpp ./sprite.cpp
cp *.h libs/SDL2-2.0.12-src/build/net.tapp.sdl/app/jni/src
cp -R glm libs/SDL2-2.0.12-src/build/net.tapp.sdl/app/jni/src
mkdir libs/SDL2-2.0.12-src/build/net.tapp.sdl/app/src/main/assets
cp *.bmp libs/SDL2-2.0.12-src/build/net.tapp.sdl/app/src/main/assets/
cd libs/SDL2-2.0.12-src/build/net.tapp.sdl
echo NDK_TOOLCHAIN_VERSION := 4.8 >> app/jni/Application.mk
echo APP_STL := c++_shared >> app/jni/Application.mk
#vagy
#./gradlew installDebug
#vagy
./gradlew assembleDebug
cd ../../../..
cp ./libs/SDL2-2.0.12-src/build/net.tapp.sdl/app/build/outputs/apk/debug/app-debug.apk ./bin/.
# manual install
#cd bin
#adb install app-debug.apk

7
compile_js.bat Normal file
View File

@ -0,0 +1,7 @@
rem source ~/SDKs/emsdk/emsdk_env.sh
em++ main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp ^
--embed-file download.bmp -o bin/game_js.html -s USE_SDL=2

11
compile_js.sh Executable file
View File

@ -0,0 +1,11 @@
#source ~/SDKs/emsdk/emsdk_env.sh
#saját fodítás
#em++ main.cpp libs/SDL2-js/lib/libSDL2.a -o game.html \
# -Ilibs/SDL2-js/include \
# -Ilibs/SDL2-js/lib
em++ main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp \
--embed-file download.bmp -o bin/game_js.html -s USE_SDL=2

View File

@ -4,4 +4,5 @@
#g++ main.cpp -Wall -o3 -o ./bin/game -Ilibs/SDL2-linux/include -Llibs/SDL2-linux/lib -lSDL2 -lSDL2main
g++ main.cpp -Wall -o3 -o ./game $(pkg-config --cflags --libs sdl2 glew)
g++ main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp \
-g -Wall -o3 -o ./bin/game $(pkg-config --cflags --libs sdl2 glew)

6
compile_mingw.bat Normal file
View File

@ -0,0 +1,6 @@
rem -g -> debug symbols
rem -o[0-3] -> optimization
rem -Wall -> all warning
g++ main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp ^
./libs/glad/src/glad.c -Wall -o3 -o ./bin/game.exe -Ilibs/SDL2-mingw/include -Llibs/SDL2-mingw/lib/x64 -Ilibs/glad/include -lSDL2 -lSDL2main -lOpengl32

15
compile_vs.bat Normal file
View File

@ -0,0 +1,15 @@
@echo off
if not defined DevEnvDir (
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
)
rem debug: /Zi (== -g)
cl main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp ^
libs\glad\src\glad.c /Febin/game-vc.exe ^
/EHsc ^
/Ilibs\SDL2-VC\include ^
/Ilibs\glad\include ^
/link /LIBPATH:libs\SDL2-VC\lib SDL2.lib SDL2main.lib opengl32.lib ^
/SUBSYSTEM:CONSOLE

View File

@ -16,9 +16,12 @@ void GameScene::event(const SDL_Event &ev) {
glViewport(0, 0, width, height);
} break;
break;
}
}
break;
}
} break;
case SDL_KEYDOWN: {
if (ev.key.keysym.scancode == SDL_SCANCODE_A) {
@ -31,8 +34,8 @@ void GameScene::event(const SDL_Event &ev) {
right = true;
}
} break;
break;
}
case SDL_KEYUP: {
if (ev.key.keysym.scancode == SDL_SCANCODE_A) {
@ -45,9 +48,11 @@ void GameScene::event(const SDL_Event &ev) {
right = false;
}
} break;
break;
}
}
}
void GameScene::update(float delta) {
if (up) {
sprite->position.y += delta * 3.0;
@ -81,12 +86,15 @@ void GameScene::update(float delta) {
sprite->position.y = 14.5;
}
}
void GameScene::render() {
glClearColor(0, 0, 0, 1);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
camera->bind();
tile_map->render();
sprite->render();
}
@ -112,30 +120,46 @@ GameScene::GameScene() {
texture = new Texture();
texture->load_image("download.bmp");
//ha a textúrának van alpha csatornája:
//texture->load_image("download.bmp", GL_RGBA, GL_RGBA);
material = new TextureMaterial();
material->texture = texture;
sprite = new Sprite();
sprite->mesh_instance->material = material;
sprite->height = 1;
sprite->width = 1;
sprite->position.x = 8;
sprite->position.y = 8;
float region_x = 7;
float region_y = 7;
sprite->region_x = region_x * (1.0 / 16.0);
sprite->region_y = region_y * (1.0 / 16.0);
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;
sprite->update_mesh();
tile_map = new TileMap();
tile_map->material = material;
tile_map->atlas_size_x = 16;
tile_map->atlas_size_y = 16;
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();
}
GameScene::~GameScene() {
delete camera;
delete sprite;
delete texture;
delete material;
delete tile_map;
delete sprite;
}

View File

@ -3,13 +3,12 @@
#include "scene.h"
#include "colored_material.h"
#include "camera.h"
#include "mesh.h"
#include "mesh_instance.h"
#include "sprite.h"
#include "texture_material.h"
#include "texture.h"
#include "texture_material.h"
#include "tile_map.h"
#include "sprite.h"
class GameScene : public Scene {
public:
@ -25,11 +24,11 @@ public:
bool up;
bool down;
OrthographicCamera *camera;
Sprite *sprite;
Camera *camera;
Texture *texture;
TextureMaterial *material;
TileMap *tile_map;
Sprite *sprite;
};
#endif // GAME_SCENE_H
#endif

73
mesh_utils.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "mesh_utils.h"
void MeshUtils::create_cone(Mesh *mesh) {
if (!mesh)
return;
uint32_t vc = mesh->vertices.size();
//eleje
mesh->add_color(1, 0, 0);
mesh->add_vertex3(0, 0.5, 0);
mesh->add_color(1,0,0);
mesh->add_vertex3(-0.5, -0.5, 0.5);
mesh->add_color(1, 0, 0);
mesh->add_vertex3(0.5, -0.5, 0.5);
mesh->add_triangle(0 + vc, 1 + vc, 2 + vc);
//bal
mesh->add_color(0, 1, 0);
mesh->add_vertex3(0, 0.5, 0);
mesh->add_color(0, 1, 0);
mesh->add_vertex3(-0.5, -0.5, -0.5);
mesh->add_color(0, 1, 0);
mesh->add_vertex3(-0.5, -0.5, 0.5);
mesh->add_triangle(3 + vc, 4 + vc, 5 + vc);
//jobb
mesh->add_color(0, 0, 1);
mesh->add_vertex3(0, 0.5, 0);
mesh->add_color(0, 0, 1);
mesh->add_vertex3(0.5, -0.5, 0.5);
mesh->add_color(0, 0, 1);
mesh->add_vertex3(0.5, -0.5, -0.5);
mesh->add_triangle(6 + vc, 7 + vc, 8 + vc);
//hátulja
mesh->add_color(1, 1, 0);
mesh->add_vertex3(0, 0.5, 0);
mesh->add_color(1, 1, 0);
mesh->add_vertex3(0.5, -0.5, -0.5);
mesh->add_color(1, 1, 0);
mesh->add_vertex3(-0.5, -0.5, -0.5);
mesh->add_triangle(9 + vc, 10 + vc, 11 + vc);
//alja
mesh->add_color(1, 0, 1);
mesh->add_vertex3(-0.5, -0.5, -0.5);
mesh->add_color(1, 0, 1);
mesh->add_vertex3(0.5, -0.5, 0.5);
mesh->add_color(1, 0, 1);
mesh->add_vertex3(-0.5, -0.5, 0.5);
mesh->add_color(1, 0, 1);
mesh->add_vertex3(0.5, -0.5, -0.5);
mesh->add_triangle(12 + vc, 13 + vc, 14 + vc);
mesh->add_triangle(13 + vc, 12 + vc, 15 + vc);
}

11
mesh_utils.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef MESH_UTILS_H
#define MESH_UTILS_H
#include "mesh.h"
class MeshUtils {
public:
static void create_cone(Mesh *mesh);
};
#endif

138
tile_map.cpp Normal file
View File

@ -0,0 +1,138 @@
#include "tile_map.h"
#include "./glm/gtc/matrix_transform.hpp"
#include "./glm/matrix.hpp"
#include "./glm/vec3.hpp"
#include "camera.h"
void TileMap::build_mesh() {
if (!mesh) {
mesh = new Mesh(2);
} else {
mesh->clear();
}
if (!data) {
//mesh->upload();
return;
}
float asx = 1.0 / atlas_size_x;
float asy = 1.0 / atlas_size_y;
for (int x = 0; x < size_x; ++x) {
int x_offset = x * size_x;
for (int y = 0; y < size_y; ++y) {
uint8_t d = data[x_offset + y];
if (d == 0)
continue;
float px;
float py;
switch (d) {
case 1:
px = 1;
py = 0;
break;
case 2:
px = 0;
py = 1;
break;
}
px /= atlas_size_x;
py /= atlas_size_y;
add_rect(x, y, px, py, asx, asy);
}
}
mesh->upload();
}
void TileMap::allocate_data() {
if (size_x <= 0 || size_y <= 0)
return;
if (data) {
delete[] data;
}
int size = size_x * size_y;
data = new uint8_t[size];
for (int i = 0; i < size; ++i) {
data[i] = 0;
}
}
void TileMap::add_rect(const int x, const int y, const float uv_x, const float uv_y, const float uv_size_x, const float uv_size_y) {
int vc = static_cast<int>(mesh->vertices.size() / mesh->vertex_dimensions);
mesh->add_uv(uv_x, uv_y);
mesh->add_vertex2(x, y + 1);
mesh->add_uv(uv_x + uv_size_x, uv_y + uv_size_y);
mesh->add_vertex2(x + 1, y);
mesh->add_uv(uv_x, uv_y + uv_size_y);
mesh->add_vertex2(x, y);
mesh->add_uv(uv_x + uv_size_x, uv_y);
mesh->add_vertex2(x + 1, y + 1);
mesh->add_triangle(vc + 1, vc + 0, vc + 2);
mesh->add_triangle(vc + 0, vc + 1, vc + 3);
}
uint8_t TileMap::get_data(const int x, const int y) const {
//3d-ben: data[(x * size_x * size_x) + (y * size_y) + size_z] etc
return data[x * size_x + y];
}
void TileMap::set_data(const int x, const int y, const uint8_t value) {
data[x * size_x + y] = value;
}
void TileMap::render() {
if (!mesh)
return;
glm::mat4 mat_orig = Camera::current_camera->model_view_matrix;
Camera::current_camera->model_view_matrix = glm::translate(Camera::current_camera->model_view_matrix, glm::vec3(position.x, position.y, 0));
Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation, glm::vec3(0, 0, 1));
Camera::current_camera->model_view_matrix = glm::scale(Camera::current_camera->model_view_matrix, glm::vec3(scale.x, scale.y, 0));
if (material)
material->bind();
mesh->render();
Camera::current_camera->model_view_matrix = mat_orig;
}
TileMap::TileMap() : Object2D() {
data = nullptr;
size_x = 16;
size_y = 16;
atlas_size_x = 1;
atlas_size_y = 1;
mesh = nullptr;
material = nullptr;
}
TileMap::~TileMap() {
if (data)
delete[] data;
}

36
tile_map.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef TILE_MAP_H
#define TILE_MAP_H
#include "object_2d.h"
#include "mesh.h"
#include "material.h"
class TileMap : public Object2D {
public:
void build_mesh();
void allocate_data();
void add_rect(const int x, const int y, const float uv_x, const float uv_y, const float uv_size_x, const float uv_size_y);
uint8_t get_data(const int x, const int y) const;
void set_data(const int x, const int y, const uint8_t value);
void render();
TileMap();
~TileMap();
uint8_t *data;
int size_x;
int size_y;
int atlas_size_x;
int atlas_size_y;
Mesh *mesh;
Material *material;
};
#endif