diff options
| -rw-r--r-- | code-style-example/style.cpp | 15 | ||||
| -rw-r--r-- | code-style-example/style.h | 58 | ||||
| -rw-r--r-- | contributing.md | 216 | ||||
| -rw-r--r-- | resource-manager/Audio_asset.cpp | 0 | ||||
| -rw-r--r-- | resource-manager/Audio_asset.h | 12 | ||||
| -rw-r--r-- | resource-manager/CMakeLists.txt | 34 | ||||
| -rw-r--r-- | resource-manager/Image_asset.cpp | 12 | ||||
| -rw-r--r-- | resource-manager/Image_asset.h | 19 | ||||
| -rw-r--r-- | resource-manager/Untitled.jpeg | bin | 0 -> 6756 bytes | |||
| -rw-r--r-- | resource-manager/constants.cpp | 10 | ||||
| -rw-r--r-- | resource-manager/constants.h | 16 | ||||
| -rw-r--r-- | resource-manager/img.png | bin | 0 -> 92742 bytes | |||
| -rw-r--r-- | resource-manager/main.cpp | 57 | ||||
| -rw-r--r-- | resource-manager/resource.h | 11 | ||||
| -rw-r--r-- | resource-manager/resource_fabricator.cpp | 30 | ||||
| -rw-r--r-- | resource-manager/resource_fabricator.h | 23 | ||||
| -rw-r--r-- | resource-manager/resource_manager.cpp | 48 | ||||
| -rw-r--r-- | resource-manager/resource_manager.h | 32 | 
18 files changed, 593 insertions, 0 deletions
| diff --git a/code-style-example/style.cpp b/code-style-example/style.cpp new file mode 100644 index 0000000..a42fca8 --- /dev/null +++ b/code-style-example/style.cpp @@ -0,0 +1,15 @@ + + +#include "style.h" + +MyClass::MyClass(int t_value) : m_value(t_value) {} + +MyClass::MyClass() { m_value = 0; } + +MyClass::~MyClass() {} + +const int MyClass::get_value() const { return m_value; } + +void MyClass::set_value(const int t_value) { m_value = t_value; } + +void MyClass::increment() { m_value += 1; } diff --git a/code-style-example/style.h b/code-style-example/style.h new file mode 100644 index 0000000..2a4022b --- /dev/null +++ b/code-style-example/style.h @@ -0,0 +1,58 @@ +/*! @file MyClass.h */ + +#ifndef MYPROJECT_MYCLASS_HPP +#define MYPROJECT_MYCLASS_HPP + +/** + * @brief example class + */ +class MyClass { + +public: +	/** +	 * @brief example constructor +	 * +	 * @param[integer] t_value example first argument +	 */ +	MyClass(int t_value); + +	/** +	 * @brief constructor example +	 * +	 */ +	MyClass(); + +	/** +	 * @brief deconstuctor example +	 * +	 */ +	~MyClass(); + +	/** +	 * @brief setter example with correct const implementation +	 * +	 * @param[const integer] t_value first argument +	 */ +	void set_value(const int t_value); + +	/** +	 * @brief getter example with correct const implementation +	 * +	 * @return const integer +	 */ +	const int get_value() const; + +	/** +	 * @brief increment member m_value +	 * +	 */ +	void increment(); + +private: +	/** +	 * @m_value basic member example +	 */ +	int m_value; +}; + +#endif diff --git a/contributing.md b/contributing.md index 40e1edd..0c24560 100644 --- a/contributing.md +++ b/contributing.md @@ -14,6 +14,7 @@  - TODO: tagging / versions +  # Code style  - Formatting nitty-gritty is handled by clang-format/clang-tidy @@ -21,6 +22,215 @@  - When using libraries of which the header include order is important, make    sure to separate the include statements using a blank line (clang-format may    sort include statements, but does not sort across empty lines). +- [Cpp practices](https://lefticus.gitbooks.io/cpp-best-practices/content/) +- [C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) +- [Google c++ style](https://google.github.io/styleguide/cppguide.html) + + +- .h basic code style with doxygen +```cpp +// code-style-example/style.cpp +/*! @file MyClass.h */ + +#ifndef MYPROJECT_MYCLASS_HPP +#define MYPROJECT_MYCLASS_HPP + +/** + * @brief example class + */ +class MyClass { + +public: +	/** +	 * @brief example constructor +	 * +	 * @param[integer] t_value example first argument +	 */ +	MyClass(int t_value); + +	/** +	 * @brief constructor example +	 * +	 */ +	MyClass(); + +	/** +	 * @brief deconstuctor example +	 * +	 */ +	~MyClass(); + +	/** +	 * @brief setter example with correct const implementation +	 * +	 * @param[const integer] t_value first argument +	 */ +	void set_value(const int t_value); + +	/** +	 * @brief getter example with correct const implementation +	 * +	 * @return const integer +	 */ +	const int get_value() const; + +	/** +	 * @brief increment member m_value +	 * +	 */ +	void increment(); + +private: +	/** +	 * @m_value basic member example +	 */ +	int m_value; +}; + +#endif +``` + +- .cpp basic code style +```cpp +// code-style-example/style.cpp + +#include "style.h" + +MyClass::MyClass(int t_value) : m_value(t_value) {} + +MyClass::MyClass() { m_value = 0; } + +MyClass::~MyClass() {} + +const int MyClass::get_value() const { return m_value; } + +void MyClass::set_value(const int t_value) { m_value = t_value; } + +void MyClass::increment() { m_value += 1; } +``` + +- when to use references + +-- If the input parameter in the function cannot accept a nullptr +```cpp +void foo::function(const std::string& name){}; +``` +-- Use a reference when you know that the object will exist for the lifetime of the reference and when nullability is not a concern. +```cpp +int x = 10; +int& ref = x; +ref = 20; +``` + +-- If a function should return a reference to an object that exists elsewhere (such as a member of a class or an element in a container), you can return a reference. +```cpp +container& get_element(std::vector<container>& vec, size_t index){ +  return vec[index]; +} +``` +- When to use pointers + +-- Use pointers when dealing with dynamic memory +```cpp +int* ptr = new int(5); +delete ptr; +``` + +-- Pointers can be nullptr, allowing them to represent "no object." Use pointers when you need to express optional ownership or reference. +```cpp +void foo::function(int* ptr){ +  if(ptr){ +    // ptr is not null +  } +} +``` + +-- When dealing with polymorphism. +```cpp +class Base {}; +class Derived : public Base {}; + +Base* obj = new Derived(); +obj->function(); +``` + + +- Do not use auto + +-- Instead of doing this +```cpp +auto s = "Hello"; +auto x = "42"s; +auto x = 42; +auto x = 42.f; +``` +-- Do this. +```cpp +std::string s = "Hello"; +std::string  x = "42"s; +int x = 42; +float x = 42.f; +``` +- Do not define constants as define but instead directly declare it. +-- This is wrong. +```cpp +#define PI 3.14159; +``` + +-- Do this. +```cpp +namespace my_project { +  class Constants { +  public: +    static constexpr double PI = 3.14159; +  }; +} +``` +- Use enum class instead of using enum only to prevent bugs +```cpp +void Print_color(int color); + +enum Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF }; +enum Product_info { red = 0, purple = 1, blue = 2 }; + +Web_color webby = Web_color::blue; + +// Clearly at least one of these calls is buggy. +Print_color(webby); +Print_color(Product_info::blue); +``` +- Instead use an enum class: +```cpp + +void Print_color(int color); + +enum class Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF }; +enum class Product_info { red = 0, purple = 1, blue = 2 }; + +Web_color webby = Web_color::blue; +Print_color(webby);  // Error: cannot convert Web_color to int. +Print_color(Product_info::red);  // Error: cannot convert Product_info to int. + +``` + +- Think about using size_t instead of using int for preventing processor mismatching. This might prevent a bug in the future. +```cpp +std::size_t i = 0; // use this instead of using a int +double x = 0.0; +``` +- comment style guide, you can use TODO:, FIXME:, NOTE:, or HACK: as admonitions when needed. + +-- Bad example +```cpp +// Draw loading screen. +draw_load_screen(); +``` +-- Better +```cpp  +// Compute the first 10,000 decimals of Pi. +// FIXME: Don't crash when computing the 1,337th decimal due to `increment` +//        being negative. +```  ## CMakeLists specific @@ -29,9 +239,15 @@    resolving merge conflicts when multiple sources were added by different    people to the same CMakeLists.txt easier. +## Doxygen style + +- [a C-style doxygen example](https://www.cs.cmu.edu/~410/doc/doxygen.html) + +  # Documentation  - All documentation is written in U.S. English +- [stanford latex style](https://web.stanford.edu/class/ee364b/latex_templates/template_notes.pdf)  - TODO  # Libraries diff --git a/resource-manager/Audio_asset.cpp b/resource-manager/Audio_asset.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/resource-manager/Audio_asset.cpp diff --git a/resource-manager/Audio_asset.h b/resource-manager/Audio_asset.h new file mode 100644 index 0000000..87fa762 --- /dev/null +++ b/resource-manager/Audio_asset.h @@ -0,0 +1,12 @@ +#pragma once + + +#include "resource.h" + + +class Audio : public Resource { + + + + +}; diff --git a/resource-manager/CMakeLists.txt b/resource-manager/CMakeLists.txt new file mode 100644 index 0000000..5c2c962 --- /dev/null +++ b/resource-manager/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.28) + +# Set C and C++ standards +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + +# Enable debug features +set(CMAKE_BUILD_TYPE Debug) +add_compile_definitions(DEBUG) + +# Project name and supported languages +project(crepe C CXX) + +# Find SDL2 +find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) + +# Add all source files to the executable +add_executable(main  +    main.cpp +    Audio_asset.cpp +    Image_asset.cpp +    resource_fabricator.cpp +    resource_manager.cpp +	constants.cpp +) + +# Include directories for headers +target_include_directories(main PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} .) + +# Link SDL2 library +target_link_libraries(main PRIVATE ${SDL2_LIBRARIES} SDL2_image::SDL2_image) + diff --git a/resource-manager/Image_asset.cpp b/resource-manager/Image_asset.cpp new file mode 100644 index 0000000..77eee2f --- /dev/null +++ b/resource-manager/Image_asset.cpp @@ -0,0 +1,12 @@ + + +#include "Image_asset.h" +#include <SDL2/SDL_surface.h> + + + +Image::~Image(){ +	if (surface) { +		SDL_FreeSurface(surface); +	} +} diff --git a/resource-manager/Image_asset.h b/resource-manager/Image_asset.h new file mode 100644 index 0000000..e7d13e9 --- /dev/null +++ b/resource-manager/Image_asset.h @@ -0,0 +1,19 @@ +#pragma once + + + +#include "resource.h" +#include <SDL_surface.h> + + + + + +class Image : public Resource { + +public: +	SDL_Surface* surface = nullptr; + +	~Image(); + +}; diff --git a/resource-manager/Untitled.jpeg b/resource-manager/Untitled.jpegBinary files differ new file mode 100644 index 0000000..aa4d289 --- /dev/null +++ b/resource-manager/Untitled.jpeg diff --git a/resource-manager/constants.cpp b/resource-manager/constants.cpp new file mode 100644 index 0000000..f9fbf5b --- /dev/null +++ b/resource-manager/constants.cpp @@ -0,0 +1,10 @@ +#include "constants.h" +#include <string> + + + +namespace crepe { + +	const std::string Constants::PNG_EXT = ".png"; +	const std::string Constants::OGG_EXT = ".ogg"; +} diff --git a/resource-manager/constants.h b/resource-manager/constants.h new file mode 100644 index 0000000..620ddf3 --- /dev/null +++ b/resource-manager/constants.h @@ -0,0 +1,16 @@ + +#pragma once + +#include <string> +namespace crepe { + +	class Constants { +	public: +		using FILE_PATH = std::string; + + +		static const std::string PNG_EXT; +		static const std::string OGG_EXT; +	 +	}; +} diff --git a/resource-manager/img.png b/resource-manager/img.pngBinary files differ new file mode 100644 index 0000000..43b1eca --- /dev/null +++ b/resource-manager/img.png diff --git a/resource-manager/main.cpp b/resource-manager/main.cpp new file mode 100644 index 0000000..62426d1 --- /dev/null +++ b/resource-manager/main.cpp @@ -0,0 +1,57 @@ + + +#include "Image_asset.h" +#include "resource_manager.h" +#include <SDL.h> +#include <SDL2/SDL_image.h> +#include <SDL_events.h> +#include <SDL_render.h> +#include <SDL_surface.h> +#include <SDL_timer.h> +#include <SDL_video.h> +#include <cstddef> + + +int main(){ +	SDL_Init(SDL_INIT_VIDEO); +	ResourceManager* rm = new ResourceManager; + + +	Image* img = static_cast<Image*>(rm->Load("../img.png")); +	//Resource* sound = rm->Load("/sound.ogg"); + +	 +	bool quit = false; +	 +	SDL_Event event; + +	SDL_Window* window = SDL_CreateWindow("Tessting resources", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); + +	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); +	SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, img->surface); + + +	while (!quit) { +		SDL_WaitEvent(&event); + +		switch (event.type) { +			case SDL_QUIT: +				quit = true; +				break; +		} + +		SDL_RenderCopy(renderer, texture, NULL, NULL); +		SDL_RenderPresent(renderer); + +		SDL_Delay(100); +	 +	} +	SDL_DestroyTexture(texture); +	SDL_DestroyRenderer(renderer); +	SDL_DestroyWindow(window); +	SDL_Quit(); + + + +	return 0; +} diff --git a/resource-manager/resource.h b/resource-manager/resource.h new file mode 100644 index 0000000..6bc44ed --- /dev/null +++ b/resource-manager/resource.h @@ -0,0 +1,11 @@ +#pragma once + + + + +class Resource{ +public: + +	virtual ~Resource() =default; + +}; diff --git a/resource-manager/resource_fabricator.cpp b/resource-manager/resource_fabricator.cpp new file mode 100644 index 0000000..03e3f74 --- /dev/null +++ b/resource-manager/resource_fabricator.cpp @@ -0,0 +1,30 @@ + + +#include <filesystem> +#include <string> + +#include "resource_fabricator.h" +#include "resource.h" +#include "Image_asset.h" +#include "Audio_asset.h" + +#include <SDL2/SDL_image.h> + + +Resource* ResourceFactory::create_resource(const Constants::FILE_PATH &file_path){ + +	std::string extension = std::filesystem::path(file_path).extension(); + + + +	if( extension == Constants::PNG_EXT ) { +		Image* img = new Image; +		img->surface = IMG_Load(file_path.c_str()); +		return img; +	} +	else if ( extension == Constants::OGG_EXT ){ +		return new Audio; +	} + +	return nullptr; +} diff --git a/resource-manager/resource_fabricator.h b/resource-manager/resource_fabricator.h new file mode 100644 index 0000000..3489341 --- /dev/null +++ b/resource-manager/resource_fabricator.h @@ -0,0 +1,23 @@ +#pragma once + + + +#include "resource.h" +#include "constants.h" + + +using namespace crepe; + + +class ResourceFactory { + +public: + +	static Resource* create_resource(const Constants::FILE_PATH& file_path); + + +}; + + + + diff --git a/resource-manager/resource_manager.cpp b/resource-manager/resource_manager.cpp new file mode 100644 index 0000000..ce4d5dc --- /dev/null +++ b/resource-manager/resource_manager.cpp @@ -0,0 +1,48 @@ + + +#include "resource_manager.h" +#include "constants.h" +#include "resource.h" +#include "resource_fabricator.h" +#include <SDL2/SDL_image.h> +#include <unordered_map> + + +ResourceManager::ResourceManager(){ +	IMG_Init(IMG_INIT_PNG); +} + +ResourceManager::~ResourceManager(){ + +	for(auto pair : m_resources){ +		delete pair.second; +	} + +	m_resources.clear(); + +	IMG_Quit(); +} + + +Resource* ResourceManager::Load(const Constants::FILE_PATH& file_path){ + +	if(m_resources.find(file_path) != m_resources.end(	)){ +		return m_resources[file_path]; +	} + +	Resource* res = ResourceFactory::create_resource(file_path); +	if(res){ +		m_resources[file_path] = std::move(res); +	} + +	return m_resources[file_path]; +} + + +void ResourceManager::Unload(const Constants::FILE_PATH& file_path){ +	std::unordered_map<Constants::FILE_PATH, Resource* >::iterator itr = m_resources.find(file_path); +	if(itr != m_resources.end()){ +		delete itr->second; +		m_resources.erase(itr); +	} +} diff --git a/resource-manager/resource_manager.h b/resource-manager/resource_manager.h new file mode 100644 index 0000000..7a86360 --- /dev/null +++ b/resource-manager/resource_manager.h @@ -0,0 +1,32 @@ +#pragma once + + + +#include <string> +#include <unordered_map> + + +#include "resource.h" +#include "constants.h" + +using namespace crepe; + + + +class ResourceManager{ + +public: + +	ResourceManager(); +	~ResourceManager(); +	 +	Resource* Load(const Constants::FILE_PATH& file_path); +	void Unload(const Constants::FILE_PATH& file_path); + + +private: +	std::unordered_map<Constants::FILE_PATH, Resource*> m_resources; + + + +}; |