diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/crepe/api/Camera.h | 11 | ||||
| -rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.cpp | 3 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 49 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.h | 27 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.cpp | 8 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.h | 4 | ||||
| -rw-r--r-- | src/example/rendering_particle.cpp | 8 | 
8 files changed, 51 insertions, 61 deletions
| diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ec94c44..151e5d9 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -2,8 +2,6 @@  #include "Color.h"  #include "Component.h" - -#include "api/Config.h"  #include "types.h"  namespace crepe { @@ -31,17 +29,18 @@ public:  	Color bg_color;  	//! pos The position of the camera in world units -	vec2 pos = Config::get_instance().win_set.pos; +	vec2 pos = {0,0};  	//! screen the display size in pixels ( output resolution ) -	vec2 screen = Config::get_instance().win_set.def_size; +	ivec2 screen = {1080,720};  	//! viewport is the area of the world visible through the camera (in world units) -	vec2 viewport = Config::get_instance().win_set.def_size; +	//vec2 viewport = {1000, 2000}; +	ivec2 viewport = {500, 1000};  	//! scale scaling factor from world units to pixel coordinates  	//! Zoom level of the camera view. -	double zoom = Config::get_instance().win_set.zoom; +	double zoom = 1.0f;  public:  	/** diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6de93f0..2723461 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -66,7 +66,7 @@ public:  	//! default window settings  	struct {  		//TODO make this constexpr because this will never change -		vec2 def_size = {1080, 720}; +		ivec2 def_size = {1080, 720};  		vec2 pos = {0, 0};  		float zoom = 1.0f;  	} win_set; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index c219dd0..27f219c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,3 +1,4 @@ +#include <cmath>  #include <memory>  #include "../util/Log.h" @@ -16,7 +17,7 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color  	  color(color),  	  flip(flip),  	  sprite_image(image), -	  aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) { +	  aspect_ratio(static_cast<double>(sprite_image->get_width()) / sprite_image->get_height()) {  	dbg_trace();  	this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 55b0082..fca3de4 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,6 +8,7 @@  #include <cmath>  #include <cstddef>  #include <functional> +#include <iostream>  #include <memory>  #include <stdexcept> @@ -18,6 +19,7 @@  #include "../util/Log.h"  #include "SDLContext.h" +#include "api/Config.h"  #include "types.h"  using namespace crepe; @@ -34,9 +36,11 @@ SDLContext::SDLContext() {  	if (SDL_Init(SDL_INIT_VIDEO) != 0) {  		throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError()));  	} + +	auto & cfg = Config::get_instance().win_set;  	SDL_Window * tmp_window  		= SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, -						   this->window.x, this->window.y, 0); +						   cfg.def_size.x,cfg.def_size.y, 0);  	if (!tmp_window) {  		throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError()));  	} @@ -106,20 +110,22 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {  	};  }  SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, -								  const double & img_scale, const vec2 & cam_scale) const { +								  const double & img_scale) const {  	int width, height; -	if (sprite.sprite_rect.w > sprite.sprite_rect.h) { -		width = static_cast<int>(sprite.width * cam_scale.x); -		height = static_cast<int>(width / sprite.aspect_ratio); +	if (sprite.width > sprite.height) { +		width = sprite.width; +		height = static_cast<int>(sprite.width / sprite.aspect_ratio);  	} else { -		height = static_cast<int>(sprite.height * cam_scale.y); -		width = static_cast<int>(height * sprite.aspect_ratio); +		height = sprite.height; +		width = static_cast<int>(sprite.height * sprite.aspect_ratio);  	} -	width *= img_scale; -	height *= img_scale; +	cout << width << " " << height << " " << " " << sprite.aspect_ratio << endl; + +	width *= img_scale * cam.zoom; +	height *= img_scale * cam.zoom;  	return SDL_Rect{  		.x = static_cast<int>((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), @@ -130,22 +136,20 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const  }  void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, -							   const double & img_scale, const Camera & cam, -							   const vec2 & cam_scale) { +							   const double & img_scale, const Camera & cam) {  	SDL_RendererFlip render_flip  		= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)  							  | (SDL_FLIP_VERTICAL * sprite.flip.flip_y));  	SDL_Rect srcrect = this->get_src_rect(sprite); -	SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale, cam_scale); +	SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale);  	SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,  					 &dstrect, angle, NULL, render_flip);  } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam, -					  const vec2 & cam_scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) {  	SDL_RendererFlip render_flip  		= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -153,32 +157,29 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const  	SDL_Rect srcrect = this->get_src_rect(sprite);  	SDL_Rect dstrect -		= this->get_dst_rect(sprite, transform.position, cam, transform.scale, cam_scale); +		= this->get_dst_rect(sprite, transform.position, cam, transform.scale);  	SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,  					 &dstrect, transform.rotation, NULL, render_flip);  } -void SDLContext::set_camera(const Camera & cam, vec2 & scale) { +void SDLContext::set_camera(const Camera & cam) {  	// resize window -	if ((int) this->window.x != (int) cam.screen.x -		|| (int) this->window.y != (int) cam.screen.y) { -		SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); -		this->window = cam.screen; +	int w,h; +	SDL_GetWindowSize(this->game_window.get(), &w, &h); +	if ( w != cam.screen.x || h != cam.screen.y) { +		SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);  	}  	double screen_aspect = cam.screen.x / cam.screen.y;  	double viewport_aspect = cam.viewport.x / cam.viewport.y; -	// decide scaling factor for world to screen -	scale = cam.screen / cam.viewport * cam.zoom; -  	SDL_Rect view; -  	// calculate black bars  	if (screen_aspect > viewport_aspect) {  		// lettorboxing +		view.h = static_cast<int>(cam.screen.y / cam.zoom);  		view.w = static_cast<int>(cam.screen.y * viewport_aspect);  		view.x = static_cast<int>(cam.screen.x - view.w) / 2;  		view.y = 0; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1a9316e..2e40b6f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,9 +11,8 @@  #include "../api/Sprite.h"  #include "../api/Transform.h" -#include "api/Camera.h" +#include "../api/Camera.h" -#include "api/Config.h"  #include "types.h"  namespace crepe { @@ -101,14 +100,14 @@ private:  	 * \param texture Reference to the Texture object.  	 * \return Width of the texture as an integer.  	 */ -	int get_width(const Texture &) const; +	int get_width(const Texture & texture) const;  	/**  	 * \brief Gets the height of a texture.  	 * \param texture Reference to the Texture object.  	 * \return Height of the texture as an integer.  	 */ -	int get_height(const Texture &) const; +	int get_height(const Texture & texture) const;  private:  	//! Will use draw,clear_screen, present_screen, camera. @@ -118,11 +117,9 @@ private:  	 * \brief Draws a sprite to the screen using the specified transform and camera.  	 * \param sprite Reference to the Sprite to draw.  	 * \param transform Reference to the Transform for positioning. -	 * \param cam_pos position of the current camera in the scene -	 * \param cam_scale multiplier for the world to screen +	 * \param cam camera of the current scene  	 */ -	void draw(const Sprite & sprite, const Transform & transform, const Camera & cam, -			  const vec2 & cam_scale); +	void draw(const Sprite & sprite, const Transform & transform, const Camera & cam);  	/**  	 * \brief Draws a particle to the screen using the specified parameters @@ -130,12 +127,11 @@ private:  	 * \param  sprite Referenceto the sprite to draw  	 * \param  pos particle position in world units   	 * \param  angle particle angle in degrees -	 * \param  cam_pos camera position in world units  	 * \param  img_scale scalar multiplier to increase image size  -	 * \param  cam_scale camera scalar for world to screen +	 * \param  cam camera of the current scene  	 */  	void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, -					   const double & img_scale, const Camera & cam, const vec2 & cam_scale); +					   const double & img_scale, const Camera & cam);  	//! Clears the screen, preparing for a new frame.  	void clear_screen(); @@ -147,7 +143,7 @@ private:  	 * \brief sets the background of the camera (will be adjusted in future PR)  	 * \param camera Reference to the Camera object.  	 */ -	void set_camera(const Camera & camera, vec2 & scale); +	void set_camera(const Camera & camera);  private:  	/** @@ -163,13 +159,12 @@ private:  	 *  	 * \param sprite Reference to the sprite to calculate rectangle  	 * \param pos the pos in world units -	 * \param cam_pos the camera position in world units +	 * \param cam the camera of the current scene  	 * \param img_scale the image multiplier for increasing img size  -	 * \param scale the multiplier for world to screen   	 * \return sdl rectangle to draw a dst image to draw on the screen  	 */  	SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, -						  const double & img_scale, const vec2 & cam_scale) const; +						  const double & img_scale) const;  private:  	//! sdl Window @@ -178,8 +173,6 @@ private:  	//! renderer for the crepe engine  	std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer; -	//! viewport for the camera window -	vec2 window = Config::get_instance().win_set.def_size;  };  } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 564fc79..beed9ce 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,7 +2,6 @@  #include <cassert>  #include <cmath>  #include <functional> -#include <iostream>  #include <stdexcept>  #include <vector> @@ -10,7 +9,6 @@  #include "../api/ParticleEmitter.h"  #include "../api/Sprite.h"  #include "../api/Transform.h" -#include "../api/Vector2.h"  #include "../facade/SDLContext.h"  #include "RenderSystem.h" @@ -30,7 +28,7 @@ void RenderSystem::update_camera() {  	for (Camera & cam : cameras) {  		if (!cam.active) continue; -		this->context.set_camera(cam, this->scale); +		this->context.set_camera(cam);  		this->curr_cam_ref = &cam;  	}  } @@ -73,13 +71,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale)  		for (const Particle & p : em.data.particles) {  			if (!p.active) continue;  			this->context.draw_particle(sprite, p.position, p.angle, scale, -										*this->curr_cam_ref, this->scale); +										*this->curr_cam_ref);  		}  	}  	return rendering_particles;  }  void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { -	this->context.draw(sprite, tm, *this->curr_cam_ref, this->scale); +	this->context.draw(sprite, tm, *this->curr_cam_ref);  }  void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 97222f3..08930b0 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,10 +77,8 @@ private:  private:  	//! Pointer to the current active camera for rendering -	Camera * curr_cam_ref = nullptr;  	// TODO: needs a better solution - -	vec2 scale; +	Camera * curr_cam_ref = nullptr;  	SDLContext & context = SDLContext::get_instance();  }; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index eec2769..680c0ac 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,13 +32,13 @@ int main(int argc, char * argv[]) {  	Color color(255, 255, 255, 255);  	Sprite & test_sprite = game_object.add_component<Sprite>( -		make_shared<Texture>("asset/spritesheet/spritesheet_test.png"), color, +		make_shared<Texture>("asset/texture/test_ap43.png"), color,  		FlipSettings{true, true});  	test_sprite.order_in_layer = 5; -	test_sprite.width = 1000; -	test_sprite.height = 500; +	test_sprite.width = 259; +	test_sprite.height = 195; -	game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; +	//game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true;  	/*  	auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ |