Skip to content

Commit

Permalink
Dev (#42)
Browse files Browse the repository at this point in the history
* Move NonCopyable into jng

* Minor fix

* Updated glad to glad2 to use wgl loader

* Add package script and some related fixes

* WIP

* WIP

* Add imgui headers to package

* Fix ecs group getting bug

* WIP

* Add ability to create entities from scripts

* Minor

* Moved camera to scene folder

* Minor fix

* Minor

* Fix D3D

* Add destroyEntity to native script api
  • Loading branch information
Kostu96 authored Jul 18, 2022
1 parent 4e700f1 commit 206b571
Show file tree
Hide file tree
Showing 40 changed files with 6,033 additions and 5,617 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Just™ Game Engine
A 3D/2D game engine developed as an educational project with hope to be usefull.

## Prerequisites
## Prerequisites:
- CMake >=3.20 (3.22 tested)
- VulkanSDK 1.3.x.x (1.3.216.0 tested)
- VULKAN_SDK environment variable has to be set to the installation directory
Expand All @@ -21,3 +21,16 @@ cmake ..
and it might Just™ work for you.

If not better instructions will come in the future...

## Usage:
For now JNG is ditributed as a package with static libraries (compiled for Visual Studio 2022).
To use it:
- Build package using /scripts/prepare_package.bat or download prebuilt from GitHub
- Set additional include directory in your project to {package_dir}/include
- Link to libraries in {package_dir}/lib/{config}, where {config} is debug or release depending on your configuration
- Copy contents of the {package_dir}/bin into your application working directory
- Define the following:
- JNG_DEBUG - [optional] if you want to use JNG debug features (eg. logging)
- SPDLOG_COMPILED_LIB - [required] - needed by spdlog library
- IMGUI_DISABLE_INCLUDE_IMCONFIG_H - [required] - needed by Dear ImGui library
- IMGUI_USER_CONFIG="jng/imconfig.hpp" - [required] - same as above
4 changes: 2 additions & 2 deletions editor/source/editor_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace jng {
square2.addComponent<SpriteComponent>().color = { 1.f, 0.f, 0.f, 1.f };
}

void EditorLayer::onUpdate(float /*dt*/)
void EditorLayer::onUpdate(float dt)
{
if (m_context.viewportWindowSize.x != m_viewportFramebuffer->getProperties().width || m_context.viewportWindowSize.y != m_viewportFramebuffer->getProperties().height) {
uint32 newViewportWidth = static_cast<uint32>(m_context.viewportWindowSize.x);
Expand All @@ -54,7 +54,7 @@ namespace jng {
m_viewportFramebuffer->bind();
jng::RendererAPI::clear({ 0.1f, 0.15f, 0.2f });

m_context.activeScene->onUpdate();
m_context.activeScene->onUpdate(dt);

m_viewportFramebuffer->unbind();
}
Expand Down
2 changes: 1 addition & 1 deletion editor/source/editor_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <jng/core/base.hpp>
#include <jng/core/layer.hpp>
#include <jng/renderer/camera.hpp>
#include <jng/scene/camera.hpp>
#include <jng/scene/entity.hpp>
#include <jng/scene/scene.hpp>

Expand Down
17 changes: 13 additions & 4 deletions include/jng/core/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
*/

#pragma once
#include <ccl/non_copyable.h>
#include <cstdint>
#include <memory>

#include "jng/debug/assert.hpp"
#include "jng/debug/log.hpp"

#define JNG_BIND_EVENT_FUNC(func) [this](auto&& ...args) -> decltype(auto) { return this->func(std::forward<decltype(args)>(args)...); }

namespace jng {

class NonCopyable
{
protected:
NonCopyable() = default;
~NonCopyable() = default;
private:
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};

template<typename T>
using Scope = std::unique_ptr<T>;
template<typename T, typename ... Args>
Expand Down Expand Up @@ -49,3 +55,6 @@ namespace jng {
};

} // namespace jng

#include "jng/debug/assert.hpp"
#include "jng/debug/log.hpp"
3 changes: 3 additions & 0 deletions include/jng/core/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace jng {
class WindowResizeEvent;
class Window;

/** Main engine class from which all client applications derive
*
*/
class Engine
{
public:
Expand Down
46 changes: 25 additions & 21 deletions include/jng/core/entry_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,28 @@ int main(int argc, char* argv[])
return 0;
}

//#if defined(JNG_WINDOWS)
//
//#include "platform/windows/windows_base.hpp"
//
//#if defined(UNICODE)
//
//int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int)
//{
// return main(__argc, __argv);
//}
//
//#else
//
//int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)
//{
// return main(__argc, __argv);
//}
//
//#endif
//
//#endif
#if defined(_WIN32)

#define WINAPI __stdcall
struct HINSTANCE__;
typedef HINSTANCE__* HINSTANCE;
typedef wchar_t* LPWSTR;
typedef char* LPSTR;

#if defined(UNICODE)

int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int)
{
return main(__argc, __argv);
}

#else

int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)
{
return main(__argc, __argv);
}

#endif

#endif
1 change: 0 additions & 1 deletion include/jng/core/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace jng {

virtual void onAttach() {}
virtual void onDetach() {}
// TODO: when Cherno won't have that feature go back to return bool to enable pause layer ???
virtual void onUpdate(float /*dt*/) {}
virtual void onImGuiUpdate() {}
virtual void onEvent(Event& /*event*/) {}
Expand Down
2 changes: 1 addition & 1 deletion include/jng/debug/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace jng {

class Log :
public ccl::NonCopyable
public NonCopyable
{
public:
static void init();
Expand Down
2 changes: 1 addition & 1 deletion include/jng/jng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "platform/window.hpp"

#include "renderer/buffers.hpp"
#include "renderer/camera.hpp"
#include "renderer/framebuffer.hpp"
#include "renderer/renderer.hpp"
#include "renderer/renderer_api.hpp"
Expand All @@ -28,6 +27,7 @@
#include "renderer/texture.hpp"
#include "renderer/vertex_array.hpp"

#include "scene/camera.hpp"
#include "scene/components.hpp"
#include "scene/entity.hpp"
#include "scene/scene.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/jng/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace jng {

class Renderer :
public ccl::NonCopyable
public NonCopyable
{
public:
static void init();
Expand Down
2 changes: 1 addition & 1 deletion include/jng/renderer/renderer2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace jng {
class Texture;

class Renderer2D :
public ccl::NonCopyable
public NonCopyable
{
public:
struct Properties
Expand Down
18 changes: 9 additions & 9 deletions include/jng/renderer/camera.hpp → include/jng/scene/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#pragma once
#include "jng/core//base.hpp"
#include "jng/core/base.hpp"

#include <glm/glm.hpp>

Expand All @@ -19,8 +19,8 @@ namespace jng {

Camera() { recalculateProjection(); }

void setOrthographic(float size, float near = -1.f, float far = 1.f);
void setPerspective(float fov, float near = 0.01f, float far = 1000.f);
void setOrthographic(float oSize, float oNear = -1.f, float oFar = 1.f);
void setPerspective(float pFOV, float pNear = 0.01f, float pFar = 1000.f);
const glm::mat4& getProjection() const { return m_projection; }

void setProjectionType(ProjectionType type) { m_projectionType = type; recalculateProjection(); }
Expand All @@ -32,16 +32,16 @@ namespace jng {
float getOrthographicSize() const { return m_orthoSize; }
float getOrthographicNear() const { return m_orthoNear; }
float getOrthographicFar() const { return m_orthoFar; }
void setOrthographicSize(float size) { m_orthoSize = size; recalculateProjection(); }
void setOrthographicNear(float near) { m_orthoNear = near; recalculateProjection(); }
void setOrthographicFar(float far) { m_orthoFar = far; recalculateProjection(); }
void setOrthographicSize(float oSize) { m_orthoSize = oSize; recalculateProjection(); }
void setOrthographicNear(float oNear) { m_orthoNear = oNear; recalculateProjection(); }
void setOrthographicFar(float oFar) { m_orthoFar = oFar; recalculateProjection(); }

float getPerspectiveFOV() const { return m_perspectiveFOV; }
float getPerspectiveNear() const { return m_perspectiveNear; }
float getPerspectiveFar() const { return m_perspectiveFar; }
void setPerspectiveFOV(float fov) { m_perspectiveFOV = fov; recalculateProjection(); }
void setPerspectiveNear(float near) { m_perspectiveNear = near; recalculateProjection(); }
void setPerspectiveFar(float far) { m_perspectiveFar = far; recalculateProjection(); }
void setPerspectiveFOV(float pFOV) { m_perspectiveFOV = pFOV; recalculateProjection(); }
void setPerspectiveNear(float pNear) { m_perspectiveNear = pNear; recalculateProjection(); }
void setPerspectiveFar(float pFar) { m_perspectiveFar = pFar; recalculateProjection(); }

glm::mat4 getVP(const glm::mat4 transform) const;
private:
Expand Down
3 changes: 2 additions & 1 deletion include/jng/scene/components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/

#pragma once
#include "jng/renderer/camera.hpp"
#include "jng/scene/camera.hpp"

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
#include <type_traits>

namespace jng {
Expand Down
2 changes: 2 additions & 0 deletions include/jng/scene/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace jng {
template<typename Component>
Component& getComponent();

Scene* getScene() { return m_sceneRef; }

bool operator==(const Entity& other) const { return m_handle == other.m_handle; }
bool operator!=(const Entity& other) const { return m_handle != other.m_handle; }
operator bool() { return m_handle != entt::null; }
Expand Down
11 changes: 8 additions & 3 deletions include/jng/scene/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
*/

#pragma once
#include "jng/renderer/camera.hpp"
#include "jng/scene/camera.hpp"

#include <entt/entt.hpp>
#include <string>

namespace jng {

class Entity;
class Event;
class SceneSerializer;

class Scene
Expand All @@ -23,8 +25,11 @@ namespace jng {
void destroyEntity(Entity entity);

Camera* getActiveCamera();

void onUpdate();

void onCreate();
void onDestroy();
void onUpdate(float dt);
void onEvent(Event& event);

template<typename Func>
void each(Func func);
Expand Down
9 changes: 9 additions & 0 deletions include/jng/scripting/native_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace jng {

class Event;
class Scene;

class NativeScript
{
public:
Expand All @@ -18,11 +21,17 @@ namespace jng {
virtual void onCreate() {}
virtual void onDestroy() {}
virtual void onUpdate(float /*dt*/) {}
virtual void onEvent(Event& /*event*/) {}
protected:
template<typename T>
T& getComponent() { return m_entity.getComponent<T>(); }

Entity createEntity(const std::string& name) { return m_entity.getScene()->createEntity(name); }
void destroyEntity(Entity entity) { return m_entity.getScene()->destroyEntity(entity); }
private:
Entity m_entity;

friend class Scene;
};

} // namespace jng
2 changes: 1 addition & 1 deletion samples/sandbox/source/game_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void GameLayer::onUpdate(float dt)
{
jng::RendererAPI::clear({ 0.1f, 0.07f, 0.07f });

m_activeScene.onUpdate();
m_activeScene.onUpdate(dt);

static float timer = 0.f;
static uint32_t frames = 0;
Expand Down
2 changes: 1 addition & 1 deletion samples/sandbox/source/game_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class GameLayer :
public:
GameLayer(const GameData& gameData);

void onEvent(jng::Event& event);
void onUpdate(float dt) override;
void onEvent(jng::Event& event) override;
private:
const GameData& m_gameData;
jng::Scene m_activeScene;
Expand Down
42 changes: 42 additions & 0 deletions scripts/prepare_package.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@echo off

REM
REM Copyright (C) 2022 Konstanty Misiak
REM
REM SPDX-License-Identifier: MIT
REM

setlocal
pushd build

set PACKAGE_DIR=.\package\
set OUTPUT_DIR=.\output\
set THIRD_PARTY_DIR=..\third_party\

cmake ..
cmake --build . --target jng --config Debug
cmake --build . --target jng --config Release

robocopy ..\include %PACKAGE_DIR%include /E /NJH /NJS
robocopy %THIRD_PARTY_DIR%entt\include\entt %PACKAGE_DIR%include\entt /E /NJH /NJS
robocopy %THIRD_PARTY_DIR%entt %PACKAGE_DIR%include\entt LICENSE /NJH /NJS
robocopy %THIRD_PARTY_DIR%glm\glm %PACKAGE_DIR%include\glm *.hpp *.inl /E /NJH /NJS
robocopy %THIRD_PARTY_DIR%glm %PACKAGE_DIR%include\glm copying.txt /NJH /NJS
robocopy %THIRD_PARTY_DIR%imgui %PACKAGE_DIR%include\imgui imgui.h LICENSE.txt /NJH /NJS
robocopy %THIRD_PARTY_DIR%spdlog\include\spdlog %PACKAGE_DIR%include\spdlog /E /NJH /NJS
robocopy %THIRD_PARTY_DIR%spdlog %PACKAGE_DIR%include\spdlog LICENSE /NJH /NJS

robocopy %OUTPUT_DIR%debug\lib %PACKAGE_DIR%lib\debug /E /NJH /NJS
robocopy %VULKAN_SDK%\Lib %PACKAGE_DIR%lib\debug vulkan-1.lib shaderc_combinedd.lib spirv-cross-cored.lib spirv-cross-glsld.lib spirv-cross-hlsld.lib /NJH /NJS

robocopy %OUTPUT_DIR%release\lib %PACKAGE_DIR%lib\release /E /NJH /NJS
robocopy %VULKAN_SDK%\Lib %PACKAGE_DIR%lib\release vulkan-1.lib shaderc_combined.lib spirv-cross-core.lib spirv-cross-glsl.lib spirv-cross-hlsl.lib /NJH /NJS

robocopy ..\assets %PACKAGE_DIR%bin\assets /E /NJH /NJS

robocopy .. %PACKAGE_DIR% LICENSE /NJH /NJS

@echo package files were put in %cd%\package\

popd
endlocal
2 changes: 1 addition & 1 deletion source/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace jng {
{
JNG_PROFILE_FUNCTION();

EventDispatcher dispatcher(event);
EventDispatcher dispatcher{ event };
dispatcher.dispatch<WindowCloseEvent>(JNG_BIND_EVENT_FUNC(Engine::onWindowClose));
dispatcher.dispatch<WindowResizeEvent>(JNG_BIND_EVENT_FUNC(Engine::onWindowResize));

Expand Down
Loading

0 comments on commit 206b571

Please sign in to comment.