Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Making stuff work between windows and osx?  (Read 2979 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-04-02 13:46:05 »

So, I've been working on my map editor for a little while now, in os x 10.7, but yesterday while I was waiting to play the next game here at the lan I'm at, I installed eclipse on my windows partition so I could code a little more on it. But when I started it, the tiles were all 1 off. As in 32 pixels off on the y-axis. :s Anyone know why there's that weird difference? (It's also rendering less tiles than in os x)

My render code is:
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  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
public void render() {
   BufferStrategy bs = this.getBufferStrategy();
   if (bs == null) {
      this.createBufferStrategy(2);
      return;
   }
   Graphics g = bs.getDrawGraphics();

   // Tiles on the screen
   int tilesAcross = (int) Math.ceil(this.getWidth() / 32) + 2;
   int tilesDown = (int) Math.ceil(this.getHeight() / 32) + 2;

   // Tile the player is on
   int ptx = this.player.x / 32;
   int pty = this.player.y / 32;

   // The offset of the tile
   int xOffset = this.player.x % 32;
   int yOffset = this.player.y % 32;

   int tilesRendered = 0;

   for (int i = 0; i < tilesAcross; i++) {
      for (int j = 0; j < tilesDown; j++) {
         tilesRendered++;
         int tx = ptx + i - tilesAcross / 2;
         int ty = pty + j - tilesDown / 2;

         int rx = i * 32 - (32 + xOffset);
         int ry = j * 32 - (32 + yOffset) + 32; //These last 32 is for the tiles to be placed correctly in windows, isn't needed in osx

         if (tx < 0 || tx >= MAP_WIDTH || ty < 0 || ty >= MAP_HEIGHT) {
            g.setColor(Color.BLACK);
            g.fillRect(rx + 16, ry, 32, 32);
            continue;
         }

         g.drawImage(map[tx][ty].sprite.image, rx + 16, ry, null);

         if (map[tx][ty].getClass() == Water.class) {
            int corner = 0;
            int side = 0;

            // top
            if (ty - 1 >= 0) {
               // left corner
               if (tx - 1 >= 0) {
                  if (map[tx - 1][ty - 1].getClass() != Water.class) corner = corner | 1;
               }
               // center
               if (map[tx][ty - 1].getClass() != Water.class) side = side | 2;
               // right corner
               if (tx + 1 < MAP_HEIGHT) {
                  if (map[tx + 1][ty - 1].getClass() != Water.class) corner = corner | 2;
               }
            }
            // middle left
            if (tx - 1 >= 0) {
               if (map[tx - 1][ty].getClass() != Water.class) side = side | 1;
            }
            // middle right
            if (tx + 1 < MAP_HEIGHT) {
               if (map[tx + 1][ty].getClass() != Water.class) side = side | 4;
            }
            // bottom
            if (ty + 1 < MAP_HEIGHT) {
               // left corner
               if (tx - 1 >= 0) {
                  if (map[tx - 1][ty + 1].getClass() != Water.class) corner = corner | 8;
               }
               // center
               if (map[tx][ty + 1].getClass() != Water.class) side = side | 8;
               // right corner
               if (tx + 1 < MAP_HEIGHT) {
                  if (map[tx + 1][ty + 1].getClass() != Water.class) corner = corner | 4;
               }
            }

            // Filters out corners that are covered by sides
            // Foo magic!
            corner &= ~((side << 3) | (side >> 1) | side);

            // Something's overlapping!
            if (corner > 0) {
               Sprite sprite = getCornerSprite(corner);
               if (sprite != null) {
                  g.drawImage(sprite.image, rx + 16, ry, null);
               }
            }
            if (side > 0) {
               Sprite sprite = getSideSprite(side);
               if (sprite != null) {
                  g.drawImage(sprite.image, rx + 16, ry, null);
               }
            }
         }

         if (this.debug) {
            g.setColor(Color.gray);
            g.drawRect(rx + 16, ry, 32, 32);
         }
      }
   }

   // Paint the player
   g.setColor(Color.red);
   g.fillRect(this.getWidth() / 2, this.getHeight() / 2, this.player.size, this.player.size);

   if (this.debug) {
      g.setFont(new Font("serif", Font.PLAIN, 12));
      g.setColor(Color.gray);
      g.fillRect(15, 5, 270, 60);
      g.setColor(Color.black);
      g.drawRect(15, 5, 270, 60);
      g.drawString("Player position: " + this.player.x + "px/" + this.player.y + "px (" + ptx + "/" + pty + ")", 20, 20);
      g.drawString("Frames per second: " + fps, 20, 35);
      g.drawString("Tiles rendered: " + tilesRendered, 20, 50);
   }

   g.dispose();
   bs.show();
   frames++;
}



Anyone got some pro tips on how to not need those extra 32 pixels(At line 31)? (And maybe tell me why I need to add 16px to the x-axis in both windows and os x, for the tiles to be placed right)

(Also, so far I've only made water and grass tiles, so that's why it's checking for Water.class Tongue )
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2012-04-02 14:59:19 »

Are you sizing the window during initialization, or are you sizing the canvas/panel/whatever inside the window? If you're sizing the window, the size parameters will include decorations such as the border and title.  Since windows and os x have different sized decorations, that could cause your ceil() computations to get off.

Offline Regenuluz
« Reply #2 - Posted 2012-04-02 17:01:03 »

Just tried to remove all the "+16" and "+32" in the render method, and changed my windowframe to:

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  
27  
package gui;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {

   public MainWindow(Canvas content) {
      this.setTitle("Map Testing");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      //Set the size
      Dimension size = new Dimension(800, 600);
//      this.setPreferredSize(size);
      content.setPreferredSize(size);
     
      this.getContentPane().add(content);
     
      this.pack();
      this.setResizable(false);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
   }
}


But I'm still getting the weird out of alignment errors in windows. (Haven't tested it in os x, since i'm here to game, rather than to code Tongue)


---
Anyone..? :/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Regenuluz
« Reply #3 - Posted 2012-04-03 17:38:08 »

No one has got a solution for this? :/
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-04-09 01:36:04 »

Using setPreferredSize(Dimension) and then packing should work. What problems are you still having?

Offline Regenuluz
« Reply #5 - Posted 2012-04-09 11:15:43 »

The same problems as before. It doesn't align up in both windows and osx. Plus it doesn't align up when I resize the window either. Which would be awesome if it did. I tried with setPreferredSize(), but that gives the same issues. :/
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-04-09 20:55:45 »

Try printing out the size of the 'content' component after you call setVisible(true) by doing "content.getSize()" and see what it tells you.

Offline Regenuluz
« Reply #7 - Posted 2012-04-10 06:11:13 »

They are what I except them to be. ^^ 800x600. Tongue
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-04-10 10:13:28 »

Then your problem is in your code, not the window sizing, because the window is the correct size Tongue

Offline Regenuluz
« Reply #9 - Posted 2012-04-10 16:13:43 »

Yeah, I figured as much xD I just can't figure out what the heck I'm doing wrong! Tongue (Which is why I pasted the render code here too ^_^)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-04-10 22:04:00 »

Looking through the render code, I see nothing that should causes differences between OS's. Try using a debugger in the problem areas on both OS's and notice what differences you see. Good luck! Smiley

Offline Regenuluz
« Reply #11 - Posted 2012-04-11 11:31:04 »

Haha, I guess I'll have to do something eventually. But it's fricking odd that it's not doing as expected. It even happens when I resize the window. :/
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 (50 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (84 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!