0
\$\begingroup\$

I'm developing an android game, and my screen is not displaying what I want it to. I've read various tutorials but they are either outdated or hard to follow. I have followed this tutorial and most of the code is identical. However, when running the game, the screen is black.

Game logic loop:

public void run(){
    while(running){
        long beforeTime = System.nanoTime();
        gameEngine.update();
        Canvas c = null;
        try{
            c = holder.lockCanvas(null);
            //synchronized (holder){
                c.drawRect(0,0,c.getWidth(),c.getHeight(),blackPaint);
                gameEngine.draw(c);
            //}
        }finally{
            if(c!=null){
                holder.unlockCanvasAndPost(c);
            }
        }

        this.sleepTime = delay-((System.nanoTime()-beforeTime)/1000000L);
        try{
            if(sleepTime>0){
                this.sleep(sleepTime);
            }
        }catch(InterruptedException e){
            Logger.getLogger(GameThread.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}

Main activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);
}

engine.draw(Canvas c):

public void draw(Canvas canvas){
    Paint bluepaint = new Paint(Color.BLUE);
    canvas.drawRect(50,50,100,100, bluepaint);
}

EDIT:

@wondra I did test the break point, and the thread does actually get started, or so I think. The app runs and there are no errors thrown to the log console. Someone also suggested it was the emulator not running properly.

@fadden I have implemented what you suggested, still nothing... does starting the view from the xml and not from the Java have to do anything to do with it? Does the instance still exists if I don't declare it in Java?

\$\endgroup\$
  • \$\begingroup\$ Have you started the thread? Place breakpoint if your gameloop is ever reached. \$\endgroup\$ – wondra Jan 17 '15 at 15:54
  • \$\begingroup\$ also, the gamme thread is started at my gameView class when the surface is created. \$\endgroup\$ – KrackLocks Jan 18 '15 at 18:22
  • \$\begingroup\$ Some key points are missing: how you access the surfaceview? How do you start the thread?. Starting view from xml/java has nothing to do with anythin. \$\endgroup\$ – fdreger Nov 17 '15 at 12:42
  • 1
    \$\begingroup\$ The black screen is how we all start. The hardest is getting the first sprite/triangle to show up. \$\endgroup\$ – v.oddou Mar 17 '16 at 1:16
  • \$\begingroup\$ Where do you swap buffers at? \$\endgroup\$ – Krythic Nov 26 '16 at 18:14
1
\$\begingroup\$

It looks like you're drawing on a SurfaceView surface. Bear in mind that the surface is a completely separate layer that, by default, sits behind everything else. The View part of the SurfaceView is a transparent "hole" that the layout code uses to adjust everything else around it. The most common way to get a black screen is to define an opaque background color for the View part.

A simple thing to try: use SurfaceView#setZOrderOnTop() to put the surface on top of everything else. You need to call this before the surface is created (onCreate()). If your rendering appears, then your render code is fine but your Views are obscuring your surface.

You may want to change your blackpaint to some other color (red) so you can see if any of it is visible.

Other tips: use Canvas#drawColor() to clear the screen, rather than a rectangle. Get rid of postInvalidate() -- by definition you are drawing on a separate layer and you do not need or want the Views to do anything. You don't need the synchronized (holder) either.

You may also want to read about game loops in the graphics architecture doc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.