Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  why call bufferStrategy.getDrawGraphics() every gameloop cycle?  (Read 2041 times)
0 Members and 1 Guest are viewing this topic.
Offline No_Germs

Junior Duke





« Posted 2005-09-06 16:12:09 »

i've noticed that most game loop look something like this:
public static void Main(String[] Args)
{
       while(true)
       {
              g = (Graphics2D) bufferStrategy.getDrawGraphics();
              //do drawing
       }
}
while i prefer:
public static void Main(String[] Args)
{
       g = (Graphics2D) bufferStrategy.getDrawGraphics();
       while(true)
       {
              //do drawing
       }
}
why is this wrong (i'm guessing it's wrong because i haven't seen any code like this)?
Offline Linuxhippy

Senior Duke


Medals: 1


Java games rock!


« Reply #1 - Posted 2005-09-06 16:31:43 »

I just assume that BufferStrategy internally is based on some type of VolatileImage and if a VI gets unuseable that its graphics-object is unuseable too.
For sure BufferStrategy does some clever caching internally to not unesscesary create tons of graphics-objects ;-)

lg Clemens
Offline Tzan

Junior Duke





« Reply #2 - Posted 2005-09-06 18:05:56 »

When you call getDrawGraphics() you are getting the graphics context that will be drawn into.
When you set up a BufferStrategy with for example 2 buffers, you have two possible locations to draw into. The BufferStrategy handles all the blitting or flipping and each time it flips, a different context is used to draw into.

So you need to get the Graphics2D object that contains the current context to draw into.

At least that is my assumption. I've never seen an official reason for getting a new g each time, but I've never seen the question before either Smiley


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2005-09-07 04:33:40 »

Always get the graphics object, paint and dispose. Each frame.

Reusing the graphics object doesn't yield a speed increase and it can lead to unlimited memory consumption on the mac. Been there... done that. There is no need to repeat my mistakes Wink

An optimisation like that (removing one cast and one get method) would only yield a speed increase if it would happen very often per frame. Keep in mind that passing a reference around is a very cheap operation. And don't waste your time with things which won't make a difference. Using a profiler clearly helps Smiley

弾幕 ☆ @mahonnaiseblog
Offline No_Germs

Junior Duke





« Reply #4 - Posted 2005-09-07 05:06:39 »

it can lead to unlimited memory consumption on the mac
how come? it doesn't make sense,that calling a method (that isn't disposal related) fewer times can cause memory consumption...
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #5 - Posted 2005-09-07 06:15:25 »

Dunno really. Looks like it accumulates some kind of data until you dispose... since that never happend it eventually went throught the roof.

弾幕 ☆ @mahonnaiseblog
Offline No_Germs

Junior Duke





« Reply #6 - Posted 2005-09-07 06:19:24 »

it's a shame there isn't a complete guide for this...
i always saw the calls to dispose on Graphics objects, but never understood why... well, thanks anyway  Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!