Skip to content

Commit

Permalink
Experiment with 3D graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 7, 2023
1 parent b81da89 commit 48cbffa
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 25 deletions.
Binary file added resources/craft_speederC.glb
Binary file not shown.
Binary file added resources/robot.glb
Binary file not shown.
Binary file added resources/ship.glb
Binary file not shown.
Binary file modified screenrec001.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ObjectState Asteroid::update()
position.x += velocity.x;
position.y += velocity.y;

wrap_position(position);
//wrap_position(position);

rotation += rotation_speed;

Expand Down Expand Up @@ -57,4 +57,7 @@ void Asteroid::draw() const noexcept
DrawCircleLines(position.x - Game::width, position.y, size, RED);
DrawCircleLines(position.x, position.y + Game::height, size, RED);
DrawCircleLines(position.x, position.y - Game::height, size, RED);

const float s = 0.1f;
DrawSphereEx(Vector3{ position.x * s, 0.0f, position.y * s }, size * 0.2f, 4, 4, WHITE);
}
5 changes: 4 additions & 1 deletion src/bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ObjectState Bullet::update()
position.x += velocity.x;
position.y += velocity.y;

wrap_position(position);
//wrap_position(position);

life--;

Expand All @@ -18,4 +18,7 @@ ObjectState Bullet::update()
void Bullet::draw() const noexcept
{
DrawCircle(position.x, position.y, 2.0f, PINK);

const float s = 0.1f;
DrawSphereEx(Vector3{ position.x * s, 0.0f, position.y * s }, 0.2f, 4, 4, WHITE);
}
5 changes: 5 additions & 0 deletions src/external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ SET(SUPPORT_FILEFORMAT_VOX OFF CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_XM OFF CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_MOD OFF CACHE BOOL "" FORCE)

SET(SUPPORT_MODULE_RMODELS ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_OBJ ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_MTL ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_GLTF ON CACHE BOOL "" FORCE)

SET(GLFW_USE_HYBRID_HPG OFF CACHE BOOL "" FORCE)
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
Expand Down
23 changes: 15 additions & 8 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ void Game::init()
asteroids->push(asteroid);
}

camera.target = Vector2{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f };
camera.offset = Vector2{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f };
camera.zoom = 1.0f;
camera.rotation = 0.0f;
camera.position = Vector3{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f, 10.0f };
camera.target = Vector3{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f, 0.0f };
camera.up = Vector3{ 0.0f, 0.0f, 1.0f };
camera.fovy = 60.0f;
camera.projection = CAMERA_PERSPECTIVE;
}

void Game::update()
Expand All @@ -42,17 +43,23 @@ void Game::update()
bullets->update();
asteroids->update();

camera.target = player->position;
camera.rotation = -player->rotation;
camera.position = Vector3 { 0.0f, 20.0f, 0.0f };
camera.target = Vector3{ player->position.x * 0.1f, player->position.y * 0.1f, 0.0f };
}

void Game::draw() noexcept
{
BeginMode2D(camera);
BeginMode3D(camera);
{
DrawCube(Vector3{ 0.0f, 0.0f, 0.0f }, 0.2f, 0.2f, 0.2f, BLUE);

player->draw();
bullets->draw();
asteroids->draw();

DrawGrid(100, 10.0f);
DrawSphere(Vector3{ 0.0f, 0.0f, 0.0f }, 1.0f, RED);

}
EndMode2D();
EndMode3D();
}
6 changes: 3 additions & 3 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Game
std::unique_ptr<ObjectCircularBuffer<Bullet>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid>> asteroids;

static constexpr int width = 480;
static constexpr int height = 270;
static constexpr int width = 960;
static constexpr int height = 540;
static constexpr float delta_time = 1.0f / 60.0f;
static constexpr int NUMBER_OF_ASTEROIDS = 10;

Expand All @@ -33,5 +33,5 @@ class Game
void draw() noexcept;

private:
Camera2D camera { };
Camera3D camera { };
};
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ static float accumulator = 0.0f;
const int window_width = Game::width;
const int window_height = Game::height;

const bool integer_scaling = true;
const bool integer_scaling = false;

void update_draw_frame()
{
Game &game = Game::get();
static RenderPass game_render_pass(Game::width, Game::height);
static RenderPass game_render_pass(Game::width * 2, Game::height * 2);
SetTextureFilter(game_render_pass.render_texture.texture, TEXTURE_FILTER_BILINEAR);
if (!game_render_pass.render_func)
game_render_pass.render_func = [&]()
{
Expand Down Expand Up @@ -59,8 +60,7 @@ void update_draw_frame()
const float screen_width_float = static_cast<float>(GetScreenWidth());
const float screen_height_float = static_cast<float>(GetScreenHeight());

float scale =
std::max(std::min(screen_width_float / (float)(Game::width), screen_height_float / (float)(Game::height)), 1.0f);
float scale = std::min(screen_width_float / (float)(Game::width), screen_height_float / (float)(Game::height));
if (integer_scaling)
scale = std::floor(scale);

Expand Down
7 changes: 4 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ void Player::draw() noexcept
sprite.position.y = position.y;
sprite.draw();

DrawCircle(sprite.position.x, sprite.position.y, 2.0f, RED);
const float s = 0.1f;
DrawModelEx(model, Vector3{ position.x * s, 0.0f, position.y * s }, Vector3{ 0.0f, 1.0f, 0.0f }, -rotation, Vector3{ 1.0f, 1.0f, 1.0f }, WHITE);
}

void Player::die()
{
{return;
lives--;
position.x = Game::width / 2.0f;
position.y = Game::height / 2.0f;
Expand Down Expand Up @@ -109,7 +110,7 @@ void Player::update()
velocity.y *= max_velocity;
}

wrap_position(position);
//wrap_position(position);

for (size_t i = Game::get().asteroids->tail; i < Game::get().asteroids->head; i++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Player
{
public:
Sprite sprite{ "resources/test.aseprite", "idle" };
Vector2 position{ Game::width / 2.0f, Game::height / 2.0f };
Model model{ LoadModel("resources/ship.glb") };
Vector2 position{ 0.0f, 0.0f };
Vector2 velocity{ 0.0f, 0.0f };
float rotation{ 0.0f };

Expand Down
8 changes: 4 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Rectangle texture_rect_flipped(const Texture2D &texture)
void wrap_position(Vector2 &position)
{
if (position.x < 0)
position.x = Game::width - 1;
position.x = Game::width * 10 - 1;

if (position.x >= Game::width)
if (position.x >= Game::width * 10)
position.x = 0;

if (position.y < 0)
position.y = Game::height - 1;
position.y = Game::height * 10 - 1;

if (position.y >= Game::height)
if (position.y >= Game::height * 10)
position.y = 0;
}

0 comments on commit 48cbffa

Please sign in to comment.