• 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
Nicholas Kong

Do all 2D games do this?

17 posts in this topic

The reason I ask is because I only want to drawn the tiles if their coordinates are "within" the screen. I figure this would improve performance of the game and be efficient code. I don't think I am optimizing...just making sure unnecessary things don't get drawn when the player is not going to see it.

 

I hear draw calls are expensive...so this is why I do this. I figure if I do not do this, the game might slow to a crawl when draw calls are being called even though the objects are not on the screen.

 

The game is a 2D platformer with a camera system. 

 

My Java code example:

 

update block:

if(getX() - camera.getX() < Screen.WIDTH)
{
draw = true;
}
else
{
draw = false;
}

draw block:

if(draw)
{
blockAnim.draw(g2D,camera);
}
Edited by warnexus
0

Share this post


Link to post
Share on other sites


Setting a flag and then going back over to draw is a waste of cycles.

 

What do you mean by waste of cycles? Does my game run slightly slower then?

0

Share this post


Link to post
Share on other sites
Apparently your flow involves making one pass to set a flag and then another pass to check the flag and draw or skip.

Unless you need to know which tiles are going to be drawn for any part of your game other than drawing (and you don’t; there are other ways to handle that kind of situation, such as by pre-calculating the left, right, top, and bottom rows and deriving that information from those metrics), make a single pass at the time of rendering to simply go over the tiles once and draw or not draw them.


L. Spiro
0

Share this post


Link to post
Share on other sites

I have many questions now.

 

1) Why is y first instead of x? And why are you using the pre-increment?

 

2) How do I know how many tiles my computer is capable of rendering?

 

Suppose my screen is 760 * 1080 in width and height respectively. My block tile is 20 * 20. So that is 38 blocks going in each row and 54 blocks going in each column. So that is 2052 blocks being drawn given my screen dimensions.

Edited by warnexus
0

Share this post


Link to post
Share on other sites

Furthermore, if you are concerned about drawcalls I would like to see how this function is implemented.

blockAnim.draw(g2D,camera);

Just from looking at that line I suspect there is much room for improvement.

0

Share this post


Link to post
Share on other sites

Furthermore, if you are concerned about drawcalls I would like to see how this function is implemented.

blockAnim.draw(g2D,camera);

Just from looking at that line I suspect there is much room for improvement.

Sure. You can see how I implemented it. I had to override the draw method.

 
for(int i = 0; i < getListSize() ;i++)
{
 
if(getCurrentFrame() <= getFrameEntries(i))
{
               g.drawImage(getImage(i), getX() - camera.getX()  , getY() - camera.getY() , null);
               break;
               }
}
0

Share this post


Link to post
Share on other sites

 

I hear draw calls are expensive...

What graphics API is being used to draw your tiles?

The "draw calls are expensive" primarily relates to DirectX/OpenGL. If you are using an intermediary API, it's quite possible it is batching up draws already, and thus may have completely different performance characteristics.

 

I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API? 

Edited by warnexus
0

Share this post


Link to post
Share on other sites


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API? 

Exactly that: the AWT canvas/bufferstrategy API (as opposed to using OpenGL directly, for example).

0

Share this post


Link to post
Share on other sites

 


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API? 

Exactly that: the AWT canvas/bufferstrategy API (as opposed to using OpenGL directly, for example).

 

Oh so I don't need to worry about efficiency then since the API is batching it up like that?

0

Share this post


Link to post
Share on other sites

I have no idea - AWT isn't exactly my area of specialty. I'm sure there are some AWT-specific performance guidelines floating around out there...

 

If you were using OpenGL, I could offer performance advice :)

0

Share this post


Link to post
Share on other sites

In the current APIs individual draw calls are expensive because for each draw call the API/Driver are potentially changing and verifying a lot of state. Batching similar draws (say, all the grass tiles, or better yet, all the tiles from the same, large texture atlas, using the same shader, etc) is one way of reducing the number of draw calls you have to make -- thus, the API/Driver overhead is amortized across all those tiles that would otherwise be drawn individually in a naive renderer. Realistically, on a modern desktop or laptop with current APIs you get a couple thousand draw calls before your CPU is completely swamped by the overhead. If you have a game running at just 640x480, using small tiles of 16x16 pixels, drawing just one densely-populated layer of tiles consumes 1200 draw calls if you do them individually. Figure two more sparsely-populated layers for objects and overhead graphics add 50% on top of that. You're 640x480 game has already consumed half of available draw calls per frame -- Now draw lots of characters, throw in some particles and UI -- you're already probably at or around the comfortable limits if your game does any interesting processing, and you haven't drawn a single off-screen tile or entity. Drawing half a screen-width extra in all directions multiplies the cost by 4x and you're way over your draw call budget.

 

On mobile platforms using mostly OpenGL ES, you can expect to make half or fewer draw calls to stay in budget.

 

Its mostly batching that's important if you're using a 3D API -- Once the GPU gets a hold of the draw call it'll chew through clipped pixels like nobody's business, and it'll reject them before running expensive pixel shader code. There's no point sending stuff to the GPU that you easily know is not in view, but you don't have to worry about being tile or pixel-perfect about it. Batching will save you far more.

 

As an aside, new style APIs like Mantle, D3D12, and the console APIs aren't so affected by draw call counts, and have other features to keep re-usable draw commands on the GPU to reduce overhead even further. In statistical analysis, the D3D12 team showed that nearly all games re-use 90% of their draw commands frame-over-frame, so my understanding is that these APIs make it possible to just re-use the command with slightly different properties (say, its transform matrix or lighting properties.), rather than rebuilding the command, sending it to the GPU and verifying it each frame.

1

Share this post


Link to post
Share on other sites
Using OpenGL I make 2D tile maps with a single triangle-strip and degenerate vertices. Don't know if it's the best practice but it gets the job done.
0

Share this post


Link to post
Share on other sites


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API? 

Ouch. Be careful.

 

That relies heavily on the Java environment it is being run on. As you probably aren't doing this on Android (I'm sure you would have tagged it and placed it in the Mobile forum) you should know some quirks.

 

The Windows JRE has some nasty performance issues in games rendering. Canvas is sometimes hardware accelerated, and sometimes not. BufferedImage particularly is notorious for switching between accelerated and non-acclerated. It can do it for several well documented reasons, such as reading from the image (hardware acceleration is write-only), or resizing the window, or moving between screens, or having Windows switch video modes. It can do it for many reasons which are seemingly random, such as another program running in the background. 

 

The runtime environment can --- for no reason your program can detect --- switch out of hardware acceleration mode. This will drop your framerate from something high (e.g. 3ms per frame) to something extremely low (e.g. 729ms per frame). This can happen at any time without any automatic notification.

 

You can monitor it yourself and suddenly notice you switched from frames-per-second to seconds-per-frame, but even once you detect it there is no way to correct it.

 

This is one of the biggest compelling reasons that people use external libraries for game graphics. Java's AWT works mostly okay for slow business graphics where a visual update can take a few moments. If you are making chess or something with slow event-driven updates you might be fine.  But if you need a solid frame rate measured with consistent double-digit milliseconds, you will not get that with AWT.

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