Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Articles & tutorials / Re: Keep screen aspect ratio with different resolutions using libGDX on: 2012-02-22 10:44:49
You argument can be turned against yourself. If 22 px is small, then why not just put some 11px black bars to the sides? Is not a big deal and allows preserving the aspect ratio.

I wanted to preserve stretch (no resizes) and aspect ratio in the beginning. But this meant to 1) force all devices to a screen resolution, or 2) make a configuration for every screen resolution. I didn't want either of those solutions, so I picked to loose my restrictions. Now I want to preserve aspect ratio, but allow a symmetric stretch (same stretch in x and y coordinates).

Though the stretch is bad (it could make your fonts look bad, for instance) at least is symmetric, which allows your "squares" to be kept as squares.
2  Discussions / General Discussions / Re: Recommend me a route... on: 2012-02-16 21:32:06
1: I recommend you to use an OpenGL library since you could benefit from your GPU graphic acceleration even if you only use 2D. If you intent to port to Android follow sproingie advice and use libGDX. If you don't want to port it to Android, I'd go for LWJGL + Slick utils.

2: sproingie already answered perfectly. Go for BSD, MIT, and Apache-like licensed libraries.

3: I'm a lover of dystopias and RPG is one of my favorite genres. I always love when something/someone small and powerless accomplish something great (like defeating a totalitarian system.) So I'll be looking forward to hear about your project :-)
3  Discussions / General Discussions / Re: Showcase appreciation on: 2012-02-14 00:15:03
I think we should call it Emo-poll Lips Sealed, since I'll want to die once I see the results for my (far-future) games.

I fourth the idea!  Grin
4  Discussions / General Discussions / Re: Showcase appreciation on: 2012-02-13 09:33:38
Quote

I don't agree with him when he says you only want professional opinion to be encouraged and patted in the head. When I look for opinion I want the hurting truth. If I want to get patted in the head I will find someone easily impressed, not a pro.

And that's one thing and to get verbally destroyed by a criticism that was intentionally sharped is another thing. Truth usually hurts by itself, don't make it more hurting on purpose.
5  Game Development / Newbie & Debugging Questions / Re: Problem with Slick 2d Image / Graphics on: 2012-02-10 13:28:11
Ooops, I forgot you are using Slick. I have never used it (except for the utils to load PNG textures).

Some random things that probably wont work (sorry :-():
  • Do you try to create the image AFTER this.getApp().start() is called?
  • The init() method is of a class inherited from org.newdawn.slick.BasicGame? Although I don't think it's relevant for this error, you could append the @Override tag to it, so you get a warning if it doesn't override for some reason.
  • If your class is inherited from *.BasicGame, do you call the super() constructor on your constructor?
6  Game Development / Newbie & Debugging Questions / Re: Problem with Slick 2d Image / Graphics on: 2012-02-10 13:09:32
Is your OpenGL context created? I think with LWJGL you have to call Display.create(), but it is some time since I last used it.

Other thing I could think of is enabling textures in OpenGL:
1  
import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D);
7  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-10 10:03:01
I've unleashed the power of the medal gatling to appreciate all your contributions.



Thanks for making this thread so helpful to me :-)
8  Game Development / Newbie & Debugging Questions / Re: Problem with Slick 2d Image / Graphics on: 2012-02-10 00:09:38
I think the problem is in the constructor of Image() (in the backtrace you see org.newdawn.slick.Image.<init>)

Are you sure player.getIcon().getWidth() and player.getIcon().getHeight() return the right size?
9  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-09 10:16:17
If you have code like this:
1  
2  
3  
4  
5  
6  
7  
public void setBounds(Rectangle bounds) {
   this.bounds = bounds;
}

public Rectangle getBounds() {
   return bounds;
}

Then all you've done is add boilerplate code that does nothing.

Stupid, stupid me! You are so right that I feel ashamed! I translated from C++ without realizing it doesn't work like this in Java.

However you've exposed to the outside that the object has a bounds. The object oriented way would be to have methods on the object that uses the bounds instead of exposing it. So instead of:
1  
2  
3  
4  
5  
public void mousePressed(MouseEvent e) {
    if (object.getBounds().contains(e.getPoint())) {
        object.activate();
    }
}

You could do:
1  
2  
3  
public void mousePressed(MouseEvent e) {
    object.activateIfPointIsInside(e.getPoint());
}


That makes sense, but I'm not sure I know how to implement it in the design I have so far :_(

Ok, the thing I get from this discussion is I have to keep reading/learning to improve my code AND kevglass/Riven advice 1) make a game, 2) make it efficient enough so it is fast, and 3) make it pretty. I'll try to advance in both paths in parallel!
10  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-09 10:04:15
@tom, @sproingie, @ReBirth: I read Item 16 of Effective Java. This item example is clear on when composition is better than inheritance. It took me long but now I'm convinced that composition is a real alternative, thanks! :-)

11  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-08 23:43:48
There is always a conflict between the right way and the easy way. [...] For me the only way to go fast is go well.

I agree with you, completely. But sometimes we can't choose the "right way" because we don't know where it is :-(
12  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-08 23:32:13
1  
2  
3  
4  
5  
// good
public void draw(SpriteBatch sb)
{
    mySprite.draw(sb);
}

I meant the first one.

That's good in this case, because all the draw methods are called from Game.draw() and I can pass the SpriteBatch parameter on them. But when things go the other way around (imagine Entity.update() needs information of SomeOtherEntity.getPosition()) then I don't see how to make it. It's just the way I know how to make things work :-(

I meant the opposite. I consider getters/setters as a bad smell and can often be symptomatic of bad design.

That's weird, I consider accessing the field directly to be a bad habit. You are breaking encapsulation for good. I understand that sometimes, when modifying great amounts of data, passing a heavy input parameter to a setter could be less efficient. But accessing the field directly by norm...

Composition. Google around and you'll find that most people favor composition over inheritance.

Hmm, I thought composition was preferred to inheritance when there is no clear is-a relationship, but in my case, MainMenu is-a Menu and Menu is-a GameState. It really don't go further than two/three levels. Also, composition has the great disadvantage of delegating all methods to the implementation (is it more tedious to maintain though might be less coupled to the superclass).


Thanks! I keep it in mind and have to read about composition with interfaces, which the author claims is the real replacement to inheritance.

Maybe it looks I'm just rejecting by default all your advices, but it's not the case. I'm reading and learning with them, even though I might not follow them too strictly. Thank you!
13  Discussions / General Discussions / Re: Hi all! on: 2012-02-08 23:10:36
Welcome aboard! :-)
14  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-08 13:05:21
You know, we should have a thread where everyone post their structure. Especially from mods.

I think it's a fantastic idea! I would learn a lot from other people structures and from other people complains about mine.
15  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-08 09:56:47
[...] Consider passing data in as parameters to methods or to the constructor instead of getting the data threw the global Game object. It will reduce coupling. So it would be ok to use it in the Menu screen but maybe avoid it in the Font class.

I don't understand that, especially: "Consider passing data in as parameters to methods or to the constructor instead of getting the data threw the global Game object". Do you mean something like this example?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// good
public void draw(SpriteBatch sb)
{
    mySprite.draw(sb);
}

// smelly
public void draw()
{
    mySprite.draw(Overlord.getGame().getSpriteBatch());
}


Or do you mean to have Overlord to return data instead of an instance of Game?
1  
2  
3  
4  
public void draw()
{
    mySprite.draw(Overlord.getSpriteBatch());
}


Avoid using setter and getters. Just make the fields public if it is a data heavy class, like tuples. If the data is read only then pass it into the constructor and make the field final.

I agree. By default I always use getters/setters and never public fields (Effective C++ can be really convincing), but for some read-only fields I think the final field + value passed as constructor parameter could be 1) logically better, 2) prettier :-)

Avoid using inheritance. Especially if you use it as a way to reuse code.

I mainly use it because of polymorphism (I have a GameState field, but store a Menu, a Level, or Cinematic, which are children of GameState).

But also for code reuse. Menu has the methods to check whether the user has touched a menu entry and to call the appropriate Command to execute. So MainMenu, PauseMenu, GameOverMenu, and so do not have to repeat that piece of code.

What is your alternative?

GameWindow
Game (has Bitmapfont, main loop and SpriteBatch)
|_____> all states (Game as constructor ref)
            |_____> entities (Game as constructor ref too, so each entity can grab delta time, keyinput, SpriteBatch which is public)

What do you accomplish by using a local reference to Game instead of having it accessed globally? For an entity-heavy game you could end up with 100+ local references to Game. Isn't it better to use only one reference that is globally accessible?

Thanks for your comments, I'm enjoying very much knowing how other people have dealt with these problems :-)
16  Discussions / General Discussions / Re: Showcase appreciation on: 2012-02-07 21:30:14
I think it's a good idea!

I think with this new etiquette rule n00b badge-avid users, like myself, will provide more feedback and less help-me-please posts.
17  Game Development / Newbie & Debugging Questions / Re: Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-07 14:42:41
@Regenuluz: Thanks! I used to keep a private reference to Game in all the classes that made use of it. But there are two problems:

1) it escalates really bad (just imagine you have to access to other two classes, more getters/setters, more fields...)

2) it breaks certain symmetry that my OCD didn't allow to happen. Imagine you use game states. Menu is a child of GameStates which in turn is parent to all kind of menus (pause, game over, main menu, etc.). Probably some menus will make use of Game while others don't. Then what? Include special Game fields in the ones that use it (apart from special constructor and/or getters and setters) which makes every Menu child different, or include a Game field in Menu, which is under-efficient.

So I ended up using a static Overlord XD

@Riven, @princec, @Damocles: Thanks for the advice! I knew about kevglass uber-rules that rule (I still have to improve GettingStarted.part 5...) and it certainly is a relief to know that important gamedevs like yourself think it is not too bad not to use it :_)
18  Game Development / Newbie & Debugging Questions / Avoiding main class to be static. Opinions about this lousy hack on: 2012-02-07 10:58:24
Hey!

I think I've done something smelly, but not sure. I want to access some of my main class, Game, fields (SpriteBatch, change current state, etc.). The solution I was using prior libGDX is to make all fields and methods of Game static (smellrometer overflowing!). But libGDX uses an instance to start the game loop, so I had to change the way I do things.

Now I have a Overlord static class which stores all the important instances (right now is only Game) and provides public static methods for accessing its fields.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Overlord
{
    private static Game game;

    public static Game getGame()
    {
         return game;
    }

    public static void setGame(Game game)
    {
        Overlord.game = game;
    }
}

I have global access to the SpriteBatch (the canvas where I draw) via Overlord.getGame().getSpriteBatch().

In a scale from "good idea" to "worse than unjustified GOTO", what do you think about it?
19  Game Development / Articles & tutorials / Re: Keep screen aspect ratio with different resolutions using libGDX on: 2012-02-06 20:32:03
Thanks for the Scaling tip. I didn't know about it. Right now it's only on nighties of libGDX and I'm using 0.9.2, but I think I'll update soon.

Also, notice that crop is a vector to center the game scene in the device screen. The variable name is not helping in this case, sorry. Looking into the Scaling enum code I think the correct way to use it is to
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
import com.badlogic.gdx.utils.Scaling;
...
Vector2 newVirtualRes= new Vector2(0f, 0f);
Vector2 crop = new Vector2(width, height);

// get new screen size conserving the aspect ratio
newVirtualRes.set(Scaling.fit.apply((float)VIRTUAL_WIDTH, (float)VIRTUAL_HEIGHT, (float)width, (float)height));

// ensure our scene is centered in screen
crop.sub(newVirtualRes);
crop.mul(.5f);

// build the viewport for further application
viewport = new Rectangle(crop.x, crop.y, newVirtualRes.x, newVirtualRes.y);

(I haven't actually tried that code, but it should work).

I have to check scene2D out. It's the second time I've seen it mentioned regarding this topic (I think both times were you) and I haven't test it yet  Lips Sealed

Sometime this night I'll modify the "tutorial" to include your comments. Thanks again!
20  Game Development / Newbie & Debugging Questions / Re: Anything similar to AS3's Flixel library? on: 2012-02-06 09:52:36
I have tried two popular libraries/frameworks for game developing using Java: Slick and libGDX. Unfortunately, I have never used Flixel, so I can't compare if they have similar features.

There are many accurate descriptions of both around the forums, made by folks that know way better than me.

Anyway, I think a good way to fall in love for a framework is reading some of its beginner tutorials. I leave you here some starting tutorials for Slick and libGDX.
21  Game Development / Newbie & Debugging Questions / Re: libGDX, BitmapFont, and OrthographicCamera on: 2012-02-05 23:06:12
I made a "tutorial" with the "keeping the aspect ratio" idea I had and took the liberty of posting it in the tutorial section (here Pointing visit it!).

All comments are welcomed!
22  Game Development / Articles & tutorials / Keep screen aspect ratio with different resolutions using libGDX on: 2012-02-05 23:03:58
Originally, this was a post in my blog ( Pointing visit it!). But I think here it will be more useful (or destructive in case it is not accurate). I took the liberty of posting it without asking. In case this is not the place or doesn't meet the minimum requirements, just tell me. I'll remove it from here, print it on paper and burn it to ashes!

[size=18pt]Keep screen aspect ratio with different resolutions using libGDX[/size]

There’s something in that “Screen Resolution” game menu that possesses me. I’ve always fancied making a game with different screen resolutions, but the task is far from trivial. These notes are the result of a weekend spent looking for the solution (with help from the JGO community). You can download the source code from here.

[size=14pt]Problem[/size]

Imagine you are developing a game and start supporting the 480×320 resolution because it fits nice in your smartphone. You align the menus, place the sprites, and do some nasty hacks (that we all have done sometimes) to make your game look pretty. In the end, you have a game that has been developed, literally, for your own phone! (or phone screen resolution). It will look distorted in other phones with different screen resolutions

What do you want to is to support multiple screen resolutions without hardcoding all the layout for every single screen resolution that exists (there are lots of them).

[size=14pt](My) Solution[/size]

The solution I’ve found it’s not TEH solution, but it works good enough for me.

I’m working with libGDX. This library has a OrthographicCamera class that fits nice for 2D games. This class is responsible to 1) define the volume of the game scene (which in OpenGL argot is called frustum) and 2) to project it orthographically into a plane: the scene image. In addition, libGDX also provides a wrapper to the OpenGL function glViewport(), which transforms the scene image obtained with the camera class into the device screen.

The plan is the following:
  • Define a virtual resolution to work with (align menus, place sprites, etc.).
  • Set the camera to use the virtual resolution.
  • Use glViewport() to adjust our scene image to the physical resolution of the device screen (keeping the aspect ratio of course).

To define the virtual resolution, it is fine to define static final fields in your AplicationListener game class (I’m using libGDX argot). The camera, a Rectangle defining our viewport, and the SpriteBatch, which all of them we will be using later, are also (non-static) fields of the class.

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class MyAwesomeGame implements ApplicationListener
{
    private static final int VIRTUAL_WIDTH = 480;
    private static final int VIRTUAL_HEIGHT = 320;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;

    private Camera camera;
    private Rectangle viewport;
    private SpriteBatch sb;


When our game starts, it will first execute the method create() and then resize(int, int) with the width and height of the window as input parameters. In create() we should initialize all the fields required further. In particular, we will initialize the camera and the SpriteBatch (canvas of each frame).

1  
2  
3  
4  
5  
6  
    @Override
    public void create()
    {
        sb = new SpriteBatch();
        camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
    }


In resize() we should setup the Rectangle that we will be using later to set the viewport. And here it is the trick. Let’s see this function slowly. First we declare and initialize some local variables.

1  
2  
3  
4  
5  
6  
7  
    @Override
    public void resize(int width, int height)
    {
        // calculate new viewport
       float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);


They are quite intuitive, for instance, aspectRatio holds the ratio width/height of the device screen (physical resolution), scale is the factor to which scale our scene image, and crop (do not confuse with crap) is the amount of pixels to be cropped from the viewport in order to keep the aspect ratio of the scene image.

Now, if aspectRatio is greater than the virtual aspect ratio it is because the physical resolution is wider (proportionally) than the virtual resolution. Therefore, we should match the height of both resolutions (virtual and physical) and crop in the X direction since our virtual scene image wont fill the whole screen. Conversely, if aspectRatio is lesser than ASPECT_RATIO then we should match the width of both resolutions and crop in the Y direction.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        viewport = new Rectangle(crop.x, crop.y, w, h);
    }

Finally, we just have to modify the render() method (which is used to render our scene, of course) to update the camera, set the viewport, and draw our objects/entities.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    @Override
    public void render()
    {
        // update camera
       camera.update();
        camera.apply(Gdx.gl10);

        // set viewport
       Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
                          (int) viewport.width, (int) viewport.height);

        // clear previous frame
       Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        // DRAW EVERYTHING
   }


And that’s it. Let’s see it in action.

[size=14pt]Some images[/size]

To illustrate this tips I’m rendering a scene that consists on two rectangles. One green that fills all the scene (just to know where exactly our scene image is), and one square red just to detect visually aspect ratio violations. We use 480×320 as our virtual resolution, as our smartphone uses it natively. Therefore, in our phone we should see everything and without distortion, just as this screenshot I just took:


Now imagine I send this awesome game to my friend @notch (any similarity with real characters/persons is fictional) which is really rich and has a smartphone with greater resolution. He will see this flawed game:


Notice that the square has been distorted into another rectangle (non-squared). My friend is loosing part of the feeling of my game! And most important, the artist that is making such awesome graphics is really pissed off…

Using the method of this tutorial he will just get the right game:


Ok, it is true. He’s not using his whole smartphone screen (btw, who told him to spend that much money in a fancy new smartphone in the first place) but at least the aspect ratio is correct and the game graphics artist is happy again.

[size=14pt]Further approximation to perfection[/size]

I have discovered nothing new, but at least I won’t doubt again how to perform this tedious but mandatory task. You must know that there are, for sure, better approaches to solve the resolution problem. For instance, I just came up with the idea of having two/three versions of the game with different aspects ratios (say 4:3, 16:9, and 16:10). Then, you viewport the layout corresponding to the aspect ratio that is closer to the physical aspect ratio, and hence, minimizing the ugly black bands.

If you have any comment/suggestion/praise/curse, do not hesitate to leave a comment here or say something in Twitter.
23  Game Development / Newbie & Debugging Questions / Re: GradientPaint with Alpha Color: Broken in Java 7? on: 2012-02-05 22:03:41
@ra4king, FYI, your game throws an NPE on startup.  Doesn't seem to affect anything though (I didn't actually play but title screen renders okay):

1  
2  
3  
4  
5  
java.lang.NullPointerException
   at com.ra4king.jdoodlejump.gui.a.p(Unknown Source)
   at com.ra4king.jdoodlejump.JDoodleJump.g(Unknown Source)
   at com.ra4king.a.e.f(Unknown Source)
   at com.ra4king.a.l.run(Unknown Source)



I have no problems playing it (except I have some pending grudge with xjump). Definitely no NPE here.
24  Game Development / Newbie & Debugging Questions / Re: libGDX, BitmapFont, and OrthographicCamera on: 2012-02-05 17:22:57
I think I was dealing with two problems at the same time:
  • Displaying the same scene in different resolutions with the same aspect ratio.
  • Displaying fonts positioned using openGL space.

I have dealt with the first problem using a fixed camera for a fixed (virtual) resolution, and modifying the viewport for each (physical) resolution. I'll try to make a blog post about this, because I think it's complicated to explain in words, but could be interesting for others (or for me if anyone thinks it's not the best way to accomplish it).

About the second problem, thanks for your insightful comments.

Quote from: Nate
You could use a camera that maps 1:1 with screen pixels. Your model could use whatever coordinate system you like, and you would convert to pixels only for drawing.

That sounds like what I want to do. When you say "you convert to pixels only for drawing" you mean I could set my sprites/fontCaches positions in my coordinates and then, in render() convert them using an custom utility class especially crafted for that? Or is there something more optimized (less custom)?

Quote from: pitbuller
But for ui where I have to do positioning etc I allways use 800x480 projection.

Yes, I'm thinking on working on 480x320 coordinates (that's what's called "pixel perfect" coordinates? I get lost easily...) and then scale using the linear filter for the font. Thanks for the code, it helps me a lot! Sometimes it's way better to use code/mathematics to explain concepts :-)
25  Game Development / Newbie & Debugging Questions / Re: libGDX, BitmapFont, and OrthographicCamera on: 2012-02-05 15:01:58
Thanks for your time!

Why I want to use OpenGL coordinates? Good question.

It all boils down to placing text and sprites in a fixed coordinate system which is easy for me to understand. For example, using the OpenGL coordinate system from -.5f to .5f I can easily calculate the middle (.25f), quarter (.125f), etc. positions without having to access to the screen resolution (ScreenSettings.WIDTH/2f, for example). In addition, my idea is to adjust the viewport to the OpenGL scene, so multiple screen resolutions have the same appearance.

This mustn't be a good way to achieve that, but can't think of a better way to support multiple resolutions without having to hardcode them ony by one.

Do you have any suggestions?
26  Game Development / Newbie & Debugging Questions / Re: libGDX, BitmapFont, and OrthographicCamera on: 2012-02-05 13:23:21
If you want to use openGl coords why you pass -100,10 coordinates?

That's just to see something on screen. If I use -.5f, .5f I see nothing, but that's the coordinate system I want to use.

Another point is that you don't set the size of the font anywhere.

You mean with BitmapFont.setScale(float) method?

Hmm, I guessed that since BitmapFont produces text by packing the font glyphs from a texture, it behaves like a texture. Following the "Projection, Viewport, and Camera" tutorial on libGDX, it seemed to me that you don't need to do scalings on the texture.
27  Game Development / Newbie & Debugging Questions / libGDX, BitmapFont, and OrthographicCamera on: 2012-02-05 12:48:46
I think this question must have been asked and answered somewhere, but I can't find it!

I'm using libGDX (mainly because of his Android painless portability). I have setup a OrthographicCamera so my OpenGL coordinates are from -1f to 1f in the Y direction and from -ar to ar in the X direction (ar is a float with the aspect ratio of the screen resolution). Some code:
1  
2  
3  
4  
5  
6  
@Override
public void resize(int width, int height)
{
    float ar = (float) width / (float) height;
    camera = new OrthographicCamera(2f*ar, 2f);
}


In the game create() overridden method I setup a font using BitmapFont.
1  
2  
3  
4  
5  
6  
@Override
public void create()
{
    sb = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("menuFont.fnt"), Gdx.files.internal("menuFont.png"), false);
}


Finally, in the render() method I update the camera, update the projection matrix in the SpriteBatch, and draw the font:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
@Override
public void render()
{
    camera.update();
    camera.apply(Gdx.gl10);
    sb.setProjectionMatrix(camera.combined);    // HERE

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    sb.begin();
        font.draw(sb, "Hola mundo", -100f, 10f);
    sb.end();
}


This way, the font gets huge on the screen! (I could only see part of a letter).

What I want to do is to use OpenGL coordinates to position the text on the screen, but without any annoying scaling. I can disable the font scaling not passing the projection matrix to the SpirteBatch, i.e., commenting line with the "// HERE" comment. But can't use OpenGL coordinates.

This must be a misconception I bear, but can't figure it out!

Thanks in advance for any comment :-)
28  Discussions / General Discussions / Re: Hi everyone! on: 2012-02-05 11:32:58
Welcome, sir!  Smiley
29  Games Center / Showcase / Re: Mini Quests on: 2012-01-08 21:50:53
I was finally able to play it in my Wildfire! It works great, though sometimes a bit slow (for instance in the rotating-fire room of Dragon quest).

The link error must have been a problem with the previous version of Android as it vanished when I updated it to a newer version.

I agree with Apo, making the corners of the screen control the movement is much more intuitive.
30  Game Development / Newbie & Debugging Questions / Re: [Java 2D Character Animation] on: 2012-01-08 13:07:23
Hey there!

I'm really new in game programming, but I think I can help. What I do is the following (you can see an old but functional engine I used for Ludum Dare 22 here). My game loop looks (in a simplified way) like this:

1  
2  
3  
4  
5  
6  
7  
8  
while(!exitRequested) {
    // some code

    Game.update(delta);
    Game.draw(delta);

    // some code
}


Game is a class that ensures that every entity (player, enemies, decorations) gets updated and drawn on screen. In Game.update(delta), delta is the number of milliseconds since last frame so the game goes at the same apparent speed in every platform. Do not pay attention to that right now.

All my entities have a custom class called Sprite. Sprite allows inserting animations (method insertAnimation()) setting animations, and updating. An animation is basically a texture (image) with all the steps in the animation and the information on how to extract each step (frame) of the animation. What we are interested in is in updating a Sprite, which is a method that sets the next step in the animation.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
class Sprite {
    // current step/frame in the animation
   private int cFrame = 0;

    // update code for Sprite class
   public void update(int delta) {
        // some code

        cFrame = getNextFrame();  // this call ensures that the next animation step is set

       // some code
   }
}


That way, all the Sprites on my games get their animation updated (Sprite.update(int)) every frame of the game (every pass on the game loop).

I'm not sure it's clear from this explanation, so if you think something is obscure just ask, I'd be willing to clarify :-)

Also, all the code I've posted is a previous version of what I do right now. If someone thinks its a bad idea to do it as I do please, tell me! I am learning too, so any advice would be perfect.
Pages: [1] 2
 

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

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!