• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ISDCaptain01

How to put objects and enemies in a 2d level?

9 posts in this topic

Okay so I made a level using a 2d map editor. Now when I get to the code I can make the world scroll relative to my characters position. So now I decide to code in a few object (coins, rings etc). I set their x and y position and find out they move along with my charcter rather than being in their own spots around the level. Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position. That seems to fix the problem, but now im experiencing slow downs. Is there a better way to do this?
0

Share this post


Link to post
Share on other sites

Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position.

 

I understand you correctly that you loaded your map into an extra "backbuffer" of the same size as your map. On this "backbuffer" you blit all other objects which are not the player by hand ( hardcode instant of dynamiclly ) . Then you blit the map to the screen?

 

if that the case i would suggest you some kind of managerclass for your other entities. The basic thought behind you seperate more the logic of an entity from the drawfunctions. 

 

The Manager handle the access to all entities. The logic manipulate the entities in that form that it looks like they would move or do what ever they supposed to do and after the logic finished her job the manager handover all objects e.g. to a seperate renderer. This same renderer should

also render you map. Then you have only to look in which order this renderer draw your objects to the screen!!!  The only diffculty is now you have to setup up an own move logic for every entity like a very small and simple AI. or just a simple and stupid move pattern.

 

 

Then should everything work unrelated to your players movement....

Edited by exOfde
0

Share this post


Link to post
Share on other sites


understand you correctly that you loaded your map into an extra "backbuffer" of the same size as your map. On this "backbuffer" you blit all other objects which are not the player by hand ( hardcode instant of dynamiclly ) . Then you blit the map to the screen?

 

Exactly

0

Share this post


Link to post
Share on other sites

 

Okay so I made a level using a 2d map editor. Now when I get to the code I can make the world scroll relative to my characters position. So now I decide to code in a few object (coins, rings etc). I set their x and y position and find out they move along with my charcter rather than being in their own spots around the level. Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position. That seems to fix the problem, but now im experiencing slow downs. Is there a better way to do this?

 

Sounds like your camera code is wrong.

 

All objects, including the player, should have a position in "global coordinates". Global coordinates are the coordinates from some fixed location in the loaded area (which might be the center of the area, or the upper left corner, or something like that).

 

Screen coordinates are relative to the location of the camera (the upper-left corner of the screen).

 

The camera itself has a global coordinate (i.e. it's at location (x,y) in the area).

 

You calculate screen coordinates by subtracting the camera position from the global coordinate of the object, and use this to draw.

screenPosOfObject = (myObject.globalPos - myCamera.globalPos);
draw(myObject, screenPosOfObject);

Keep positions in global positions, and only convert them to screen positions on-the-fly when you need them - it won't slow you down.

 

Don't use the buffer idea; as you found out, that'd burn through your framerate faster than napalm. wink.png

 

 

Ill try your suggestions, but would using mutliple threads help the framerate? Its chugging with my current algorithm

0

Share this post


Link to post
Share on other sites

Ill try your suggestions, but would using mutliple threads help the framerate?


No, definitely not. 2D graphics are normally really really fast. smile.png

Multithreading doesn't gain instant speed - multithreading makes your code more complex, and done wrongly, it can even slow down your program or introduce more bugs that are even harder to find. If your 2D drawing is going slowly, then most likely something is wrong in the code, and that problem needs to be fixed directly, not coded over.

 

If I'm understanding your problem correctly, instead of drawing your level and items onto a back-buffer, and then drawing the back-buffer relative to your player, you need to just draw the objects relative to your camera in the first place (my previous post explains how). No extra buffers are necessary except your API's built-in backbuffer that prevents flickering - but you shouldn't ever have to touch that directly.

0

Share this post


Link to post
Share on other sites

I got rid of the extra buffer, performance is back to normal. But im back at the start.

 

Okay here is what I do:

 

Here is how I set the camera up first of all:

//update the map scroll position
		//camera coordinates
		mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
		mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;

This is how I keep the camera on the screen:

//Avoid moving beyond the map edge
		if(mapxoff < 0) 
			mapxoff = 0;
		if(mapxoff > mapwidth*mapblockwidth - WIDTH)
			mapxoff = mapwidth*mapblockwidth - WIDTH;
		if(mapyoff < 0)
			mapyoff = 0;
		if(mapyoff > mapheight*mapblockheight - HEIGHT)
			mapyoff = mapheight*mapblockheight - HEIGHT;

Next this is how I set up the rings through the level

void initializeRings()
{
	int n;
	for(n = 0; n < RINGS; n++)
	{
		rings[n] = new SPRITE;
		rings[n]->alive = 1;
		rings[n]->x = n * 10;
		rings[n]->y = 340;
		rings[n]->curframe = 0;
		rings[n]->maxframe = 4;
		rings[n]->framecount = 0;
		rings[n]->framedelay = 7;
		rings[n]->width = ring_images[n]->w;
		rings[n]->height = ring_images[n]->h;
	}
}

Now this is how I draw the rings to level

void drawRings()
{
	int n;

	//loop through all ring object
	for(n=0; n<RINGS; n++)
    {
		//If the rings have not been collected yet
       if(rings[n]->alive)  
       { 
		   //Animate each ring
           if(++rings[n]->framecount > rings[n]->framedelay)
           {
                if(++rings[n]->curframe > rings[n]->maxframe)
                rings[n]->curframe = 0;
                rings[n]->framecount = 0;
           }
		  
		   //now draw each ring to the buffer
           draw_sprite(buffer, ring_images[rings[n]->curframe], rings[n]->x, rings[n]->y);
       }
    }
}

So what you are saying is that I get each ring's x and y position like this?

 

rings[n]->x - mapxoff;

rings[n]->y - mapyoff;

 

Hmm  I cant seem to visualize it

My screen is 640*480

 

I wanna draw a ring at say coordinate 100, 100;

so 100 - maxoff?

 

servant, if you don't mind, can you give me a light code example using my rings objects?

Edited by ISDCaptain01
0

Share this post


Link to post
Share on other sites

One easy pitfall is developing on the wrong machine. If you're building a 2D game from a massive gaming rig with a 2GB video card and eight cores, it's depressingly easy to end up with a sprite game that won't run on a two-year-old computer. If you can, make sure you're using an older system... that way you discover all the bottlenecks like the one you described above, instead of your players. ;)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0