sfml engine thing I'm working on

The Partridge Family were neither partridges nor a family. Discuss.
codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

sfml engine thing I'm working on

Post by codei » November 3rd, 2017, 4:30 am

Hi, I'm new here.

Here is the game thing I'm working on using the SFML library, feel free to give feedback

currently it has a text class, map manager, texture manager, and entity system
I am planning on adding a menu system with buttons and other gui things, and dialogue boxes. Oh and collision

controls: WASD

you can't really do much and it is very limited right now

I'm new to c++, so if you see any mistakes let me know :D
Last edited by codei on June 20th, 2020, 2:31 pm, edited 1 time in total.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: sfml engine thing I'm working on

Post by MrGodin » November 3rd, 2017, 3:35 pm

Just browsing through the code i found this in Engine.hpp : const bool collision = true;
This means you cannot change this value to false . I haven't ran it but from what i've seen in the code it looks a reasonable start.
Curiosity killed the cat, satisfaction brought him back

codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

Re: sfml engine thing I'm working on

Post by codei » November 3rd, 2017, 4:13 pm

MrGodin wrote:Just browsing through the code i found this in Engine.hpp : const bool collision = true;
This means you cannot change this value to false . I haven't ran it but from what i've seen in the code it looks a reasonable start.
Ah, I haven't implemented collision yet. I was just laying out all the variables I need.

codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

Re: sfml engine thing I'm working on

Post by codei » November 4th, 2017, 6:13 pm

I just recently implemented all the code to make the camera stop at the edge of the map, which also works when it is zoomed in.

You can see it in action here:
Image

Not really a huge improvement, but it took a while to figure out.

Code: Select all

//I will never touch this code again so long as it doesn't break
void engine::cam_follow()
{
	//divide screen width and height by zoom level in header file
	int dsw = win->getSize().x / zoom, dsh = win->getSize().y / zoom;
	
	//divide again for camera centering on player
	int vx = dsw / 2, vy = dsh / 2;
	
	//player entity pointer
	rpg2d::entity *e = map_man->current_map()->get_player();
	
	//set the camera values
	cam_x = e->x - (vx / 2);
	cam_y = e->y - (vy / 2);
	
	int mw = map_man->current_map()->width_pixels();
	int mh = map_man->current_map()->height_pixels();
	
	//this code makes the camera stop at the edge of the map, don't even ask
	if(mw > dsw && mh > dsh)
	{
		if(cam_x - (vx / 2) < 0) cam_x = (vx / 2);
		if(cam_y - (vy / 2) < 0) cam_y = (vy / 2);
		if((cam_x - (vx / 2)) + dsw > mw) cam_x = mw - dsw + (vx / 2);
		if((cam_y - (vy / 2)) + dsh > mh) cam_y = mh - dsh + (vy / 2);
	}
	
	//idk why these variables are called def
	float def_x = cam_x - (vx / 2), def_y = cam_y - (vy / 2);
	
	//finally, apply zoom to sfml view
	view->reset(sf::FloatRect(def_x, def_y, dsw, dsh));
}

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: sfml engine thing I'm working on

Post by MrGodin » November 5th, 2017, 3:24 am

This is what i do to focus a camera on a position

Code: Select all

void Camera::UpdatePosition(Vec2f& in_pos)
{
	m_pos = in_pos - m_center ;
	
	m_pos.x = __max(m_pos.x, m_mapFrame.left);
	m_pos.y = __max(m_pos.y, m_mapFrame.top);
	m_pos.x = __min(m_pos.x, m_mapFrame.right - m_screen_width);
	m_pos.y = __min(m_pos.y, m_mapFrame.bottom - m_screen_height);
	m_pos.y = __max(m_pos.y, 0.0f); 
}
m_mapFrame is a rectangle encapsulating the game "world"
The camera position in this case is top/left of the viewport/window . if we pass in an object to focus on, back off the camera half extents (center of viewport) and this will keep the object in the middle of the screen so to speak. Cap the camera position relative to the world dimensions.
I am sure there are other ways of doing this, but it works for me.
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: sfml engine thing I'm working on

Post by albinopapa » November 5th, 2017, 3:40 am

MrGodin's stuff doesn't account for zoom level, but the process should be the same. You don't want your viewport to go outside the bounds of your level. Update position of camera, check that the viewport rectangle is not overlapping the level rectangle, add or subtract the amount, if any, overlap to the position of the camera. Remember that your logic code should be separate from your drawing code. If you do this, your logic code should be pretty straight forward.

One way to look at it is when you "zoom in" your viewable area is smaller, like making the viewport rectangle smaller, so maybe do only the calculations needed to resize the rect. Then in the draw portion, you'd have to scale up the graphics assets and such to the zoom factor. Just a thought, but if it's working and you like how it is, then just move on and deal with it later when you have gotten to the point where you want to refactor.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

Re: sfml engine thing I'm working on

Post by codei » November 6th, 2017, 12:42 am

MrGodin wrote:This is what i do to focus a camera on a position

Code: Select all

void Camera::UpdatePosition(Vec2f& in_pos)
{
	m_pos = in_pos - m_center ;
	
	m_pos.x = __max(m_pos.x, m_mapFrame.left);
	m_pos.y = __max(m_pos.y, m_mapFrame.top);
	m_pos.x = __min(m_pos.x, m_mapFrame.right - m_screen_width);
	m_pos.y = __min(m_pos.y, m_mapFrame.bottom - m_screen_height);
	m_pos.y = __max(m_pos.y, 0.0f); 
}
m_mapFrame is a rectangle encapsulating the game "world"
The camera position in this case is top/left of the viewport/window . if we pass in an object to focus on, back off the camera half extents (center of viewport) and this will keep the object in the middle of the screen so to speak. Cap the camera position relative to the world dimensions.
I am sure there are other ways of doing this, but it works for me.
That looks neat, but I'm not used to doing it that way. I'd rather work with separate x and y values to make things easier to look at.

Are you using SFML as well?

codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

Re: sfml engine thing I'm working on

Post by codei » November 6th, 2017, 12:44 am

albinopapa wrote:MrGodin's stuff doesn't account for zoom level, but the process should be the same. You don't want your viewport to go outside the bounds of your level. Update position of camera, check that the viewport rectangle is not overlapping the level rectangle, add or subtract the amount, if any, overlap to the position of the camera. Remember that your logic code should be separate from your drawing code. If you do this, your logic code should be pretty straight forward.

One way to look at it is when you "zoom in" your viewable area is smaller, like making the viewport rectangle smaller, so maybe do only the calculations needed to resize the rect. Then in the draw portion, you'd have to scale up the graphics assets and such to the zoom factor. Just a thought, but if it's working and you like how it is, then just move on and deal with it later when you have gotten to the point where you want to refactor.
I actually did the zooming accidentally by using the divided screen width and height values on the reset function in sf::View. This is why it was so confusing as to why it wasn't working until I realized I had to remove the call to sf::View::zoom(). Such a time waste, but at least I got it working perfectly fine.

codei
Posts: 8
Joined: November 2nd, 2017, 9:09 pm

Re: sfml engine thing I'm working on

Post by codei » November 6th, 2017, 1:11 am

I have just implemented entity sorting and the game still performs surprisingly well, even with over 1000 entities loaded.

Image

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: sfml engine thing I'm working on

Post by chili » November 6th, 2017, 1:16 am

Yeah, you will find that stuff at the entity level like that costs a lot less than you might originally thing. This is why I constantly scold people for premature optimization at that level. It often just isn't worth it, and it's better to write clean, scalable, idiomatic code at that that level, and save the optimized hacks for just the few portions where it really matters.
Chili

Post Reply