Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
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  
  Timebased tile scrolling looks sloppy  (Read 1405 times)
0 Members and 1 Guest are viewing this topic.
Offline Zig-Zag

Senior Newbie





« Posted 2007-03-02 16:05:59 »

I try to implement a litte tile based 'engine' but by now the scrolling is really a mess.

The main loop is a simple
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void loop() {
      long lastLoopTime = System.nanoTime();
      while (true) {
         long delta = System.nanoTime() - lastLoopTime;
         lastLoopTime = System.nanoTime();
         logic(delta);
         draw();  
      }
   }


the logic(delta) is also rather simple
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   public void logic(long delta) {
      if (keys[KeyEvent.VK_SHIFT]) { dx = 640; dy = 480;}
      else { dx = 2*64; dy = 2*48; }
     
      if (keys[KeyEvent.VK_LEFT])      map.scroll((-delta * dx) / ONE_SECOND, 0);
      if (keys[KeyEvent.VK_RIGHT])      map.scroll((delta * dx) / ONE_SECOND, 0);
      if (keys[KeyEvent.VK_UP])      map.scroll(0, (-delta * dy) / ONE_SECOND);
      if (keys[KeyEvent.VK_DOWN])      map.scroll(0, (delta * dy) / ONE_SECOND);      
   }

   public void scroll(double dx, double dy) {
      screenAnchor.x += dx;
      screenAnchor.y += dy;
     
      // prevent form leaving the map ....
   }

where ONE_SECOND is on sec in nano secs (thus 1000*1000*1000) and dx/dy the moving speed in pixel/sec and screenAnchor a point representing the first viewable point in worldspace...

and finally draw() just calls the map.render method in a double buffered enviroment created with createBufferStrategy(2);
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
   public void draw() {
      Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
      g.setColor(Color.CYAN);
      g.fillRect(0,0,WIDTH,HEIGHT);
      map.render(g);
      fps.display(g);
      g.dispose();
      strategy.show();
   }

   public void render(Graphics2D g) {
      int tiles_x = screenSpace.width / tileSize.width;
      int tiles_y = screenSpace.height / tileSize.height;
      int sa_x = (int)screenAnchor.x;
      int sa_y = (int)screenAnchor.y;
      int ox = sa_x / tileSize.width;
      int oy = sa_y / tileSize.height;
      for(int y=0;y<tiles_y+1;y++) {
         for(int x=0;x<tiles_x+1;x++) {
            g.drawImage(map[oy+y][ox+x].image,
                  x * tileSize.width - sa_x % tileSize.width,
                  y * tileSize.height - sa_y % tileSize.height,
                  null);
         }
      }
   }


If you wanna see what i mean with 'sloppy' there is a webstart
Maybe you can tell me what happens wrong with my code Sad

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2007-03-02 18:45:01 »

Works fine here.  Scrolling is smooth and at 140 FPS.

Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #2 - Posted 2007-03-02 18:49:43 »

Ditto: 192 fps

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #3 - Posted 2007-03-03 10:35:40 »

works fine here too, ~175fps

Offline Kova

Senior Duke





« Reply #4 - Posted 2007-03-03 13:19:29 »

scrolling works at ~190fps, but I can clearly see it is not smooth sometimes, every half a second or so I notice a small jump or glitch
Offline Zig-Zag

Senior Newbie





« Reply #5 - Posted 2007-03-03 14:34:48 »

Any Idea what can cause this behavior? On my maschine its even worse then Kova describes it.

About the fps:
I will get ~180 with the version released, about ~270 when useing System.setProperty("sun.java2d.d3d","True"); [opengl will not work at all] and whats kinda curious: when changing the max amount of different tiles (which are strored in a simple array) form 192 to 6 about ~500 fps.
Offline Coinerson

Junior Duke




Introducing the world's cutest zombie, Timmy


« Reply #6 - Posted 2007-03-07 05:00:35 »

smooth and at 170 fps. And err.. GC maybe? Doubt it though, unless your doing a lot more than nessisary and throwing it all in tenured.
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.

theagentd (18 views)
2014-10-25 15:46:29

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

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

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

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

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

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

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

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

BurntPizza (45 views)
2014-10-11 23:10:45
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!