Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Slick + LWJGL = Flicker?  (Read 1980 times)
0 Members and 1 Guest are viewing this topic.
Offline coltonoscopy

Junior Member


Medals: 2



« Posted 2012-07-27 08:26:10 »

Hello, everybody! Been a while ;]

I converted my game engine's code from Java2D to Slick and LWJGL completely, and it seems to be working about 70% correctly but with a few things I'm trying to work out. Namely, I'm having a huge amount of flickering when rendering anything to the screen.

Everything I'm rendering is a Slick2D Image, though it's being rendered to an LWJGL Display context. The reason for this is I converted my code first to LWJGL then realized there were things in Slick I wanted, so I converted all of the Image code and the like to Slick's code, as well as Slick's Animations, SpriteSheets, etc. while retaining the LWJGL Display, figuring since Slick is essentially based on LWJGL, they would still work together.

The point of the question is: is there a particular issue with rendering Slick Images to an LWJGL Display window? I have been trying to figure it out for myself but to no avail yet. I would greatly appreciate the help! If any code is necessary, I will be happy to post it, but it seems to me like others more experienced with the libraries would probably find this question easy to answer regardless of such. Thanks!

Best regards,
Colton

Straight flippin.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #1 - Posted 2012-07-27 10:41:10 »

better not to mix LWJGL and Slick2D code unless you really know what's going on under the hood of Slick2D. If you are using Slick2D, better to drop all the LWJGL code and use just the Slick2D API's.
Online Cero
« Reply #2 - Posted 2012-07-27 13:49:31 »

could be an issue we encounter and fixed by using our own image class instead Slicks (just copied the Slick one and changes it a bit)

How does look/behave exactly ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-07-27 15:00:45 »

While it's true Slick was never designed to be used alongside low-level GL calls, it's entirely possible to do so granted you take care of a few things.

If you're comfortable with OpenGL, I'd personally suggest rolling your own sprite renderer, as it may lead to better performance (i.e. using VBOs instead of immediate mode) and more reliability.

Show us your rendering code -- there's not much we can do without it.

Offline theagentd
« Reply #4 - Posted 2012-07-27 18:12:45 »

I believe you're manually calling Display.update(), right? Slick2D also calls that, most likely resulting in every other screen being black = flickering. Let Slick2D handle the buffer swapping or drop Slick2D altogether.

Myomyomyo.
Offline coltonoscopy

Junior Member


Medals: 2



« Reply #5 - Posted 2012-07-27 19:31:21 »

Thanks all of you for your replies! :]

better not to mix LWJGL and Slick2D code unless you really know what's going on under the hood of Slick2D. If you are using Slick2D, better to drop all the LWJGL code and use just the Slick2D API's.
I was thinking along the same lines, only because I didn't realize just how many tools Slick has that I was programming myself before and which are done more efficiently since it interfaces with OpenGL rather than Java's graphics libraries.

While it's true Slick was never designed to be used alongside low-level GL calls, it's entirely possible to do so granted you take care of a few things.

If you're comfortable with OpenGL, I'd personally suggest rolling your own sprite renderer, as it may lead to better performance (i.e. using VBOs instead of immediate mode) and more reliability.

Show us your rendering code -- there's not much we can do without it.

To be honest, I'm not terribly comfortable with OpenGL just yet (been doing some reading and tutorials and such, which has been helping but only scratching the surface). I will post my rendering code below. :]

could be an issue we encounter and fixed by using our own image class instead Slicks (just copied the Slick one and changes it a bit)

How does look/behave exactly ?

As theagentd described, it looks exactly as if every other frame is black, so there is a constant flicker on the screen.

I believe you're manually calling Display.update(), right? Slick2D also calls that, most likely resulting in every other screen being black = flickering. Let Slick2D handle the buffer swapping or drop Slick2D altogether.
You're right, I am, and so I got rid of the Display.update() call in my game loop and it ended up crashing the game and giving me only a black screen, unfortunately. I was probably supposed to do something else as well, wasn't I? :p

Here's the relevant rendering code:

This is the core game loop.
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  
public void gameLoop() 
    {
        long startTime = System.currentTimeMillis();
        long currTime = startTime;
       
        // initialization code OpenGL
       glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D);

        while (isRunning)
        {
            long elapsedTime =
                System.currentTimeMillis() - currTime;
            currTime += elapsedTime;
           
            glClear(GL_COLOR_BUFFER_BIT);

            // update
           update(elapsedTime);

            // draw the screen
           draw();
           
            while (!Display.isCloseRequested())
            {
                Display.update();
               
                // sync the Display so it maintains 60 frames per second
               Display.sync(60);
            }
           
            isRunning = false;
        }
       
        Display.destroy();
        System.exit(0);
    }


This method is the core draw method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void draw() 
    {
        player = (Player)map.getPlayer();
        renderer.draw(map, Display.getWidth(), Display.getHeight());
        guiManager.draw();
       
        if (inventoryOpen)
        {
            guiManager.renderInventory();
        }
       
        if (chestOpen)
        {
            guiManager.renderChest(currentChest);
        }
    }


This is the renderer's draw method, which is called from the core draw method, just as an example.
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  
public void draw(TileMap map, int screenWidth, int screenHeight)
    {
        Sprite player = map.getPlayer();
        int mapWidth = tilesToPixels(map.getWidth());
        int mapHeight = tilesToPixels(map.getHeight());

        // get the scrolling position of the map
       // based on player's position
       int offsetX = screenWidth / 2 -
            Math.round(player.getX()) - TILE_SIZE;
        offsetX = Math.min(offsetX, 0);
        offsetX = Math.max(offsetX, screenWidth - mapWidth);

        // get the y offset to draw all sprites and tiles
       int offsetY = screenHeight / 2 -
            Math.round(player.getY()) - TILE_SIZE;
        offsetY = Math.min(offsetY, 0);
        offsetY = Math.max(offsetY, screenHeight - mapHeight);
       
        /*int offsetY = screenHeight -
            tilesToPixels(map.getHeight()); */

        // draw parallax background image
       int bx = offsetX *
            (screenWidth - background.getWidth()) /
            (screenWidth - mapWidth);
        int by = screenHeight - background.getHeight();

        background.draw(bx, by);

        // draw the visible tiles
       int firstTileX = pixelsToTiles(-offsetX);
        int lastTileX = firstTileX +
            pixelsToTiles(screenWidth) + 1;
       
        int firstTileY = pixelsToTiles(-offsetY);
        int lastTileY = firstTileY + pixelsToTiles(screenHeight) + 1;
       
        for (int y = firstTileY; y < lastTileY; y++)
        {
            for (int x = firstTileX; x <= lastTileX; x++)
            {
                Image imageF = map.getTileF(x, y);
                Image imageB = map.getTileB(x, y);
               
                // draw background tiles
               if (imageB != null)
                {
                    imageB.draw(tilesToPixels(x) + offsetX, tilesToPixels(y) + offsetY);
                }
               
                // draw foreground tiles
               if (imageF != null)
                {
                    imageF.draw(tilesToPixels(x) + offsetX, tilesToPixels(y) + offsetY);
                }
            }
        }

        // draw player
       player.draw(Math.round(player.getX() + offsetX), Math.round(player.getY()) + offsetY);

        // draw sprites
       Iterator i = map.getSprites();
        while (i.hasNext())
        {
            Sprite sprite = (Sprite)i.next();
            int x = Math.round(sprite.getX()) + offsetX;
            int y = Math.round(sprite.getY()) + offsetY;
            sprite.draw(x, y);

            // wake up the creature when it's on screen
           if (sprite instanceof Creature &&
                x >= 0 && x < screenWidth)
            {
                ((Creature)sprite).wakeUp();
            }
        }
    }


Thanks for all the help, everyone! I'm really liking Slick and LWJGL at this point and I don't mind dropping LWJGL if I have to, considering Slick is basically an extended version of it; the features the library offers is a breath of fresh air after studying how engines work and how to program all my own stuff for 2 years. lol

Colton

Straight flippin.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2012-07-27 19:58:34 »

It's flickering because you are alternating between the front buffer and the back buffer, since you only draw once.

Display.update() and Display.sync(60) are supposed to be called once after updating and rendering on the screen. Display.isCloseRequested() returns true only when the window X button is pressed. This means that your while loop runs infinitely until you close the screen.

Instead, do this:
1  
2  
3  
4  
5  
6  
7  
8  
while(!Display.isCloseRequested()) {
    glClear...
    update(elapsedTime);
    draw();
   
    Display.update();
    Display.sync(60);
}

Offline coltonoscopy

Junior Member


Medals: 2



« Reply #7 - Posted 2012-07-27 20:04:41 »

Shocked

Wow, I'm surprised I didn't see that before. I think that came about because I added in the update, draw, and other stuff after I replaced my old loop with the new one when converting the code. Thanks so much, ra4king! Workin beautifully!

Colton

Straight flippin.
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.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!