rectclip problems

Hello,
i made a scrolling area for a tile background. That area is included in a certain rectangle. I set that rectangle as a clip region, so when the scrolling area is scrolled, only that rectangle is redrawn. But now i wonder how i could draw something around that clip region.
Thanx for help :stuck_out_tongue:

I guess my topic is stupid again ???

I don’t get your problem. To paint outside the clip then just set the clip back to what it was.

seems you understood. I tryed what u said :
when i initiate the whole screen, i draw around the future scrolling area (a gradient) then i set the clip area to the scrolling area. But the surrounding area flickers ! It shouldn’t be redrawn :-o !
Dunno wether it’s a good idea to give my code cause it’s not very short :-\

anyway thank you for your help.

If its flickering, then yes it must be getting painted outside the clip. Are you double-buffering with the window you are painitng on? Post the painting code you’re using.

If you are using an accelerated surface as a back buffer, and you want to only repaint dirty areas of the screen - you will have to take into consideration the underlying surface being lost (forcing a complete repaint). Also, if you are using a BufferStrategy with more than 1 back buffer (perhaps page flipping with either 2/3 pages), you can’t feasibly do dirty repaints at all.

Really thanks for your replies !


public void gameLoop()
	{
		int lastLoopTime = (int) System.currentTimeMillis();
		fpsCounter=0;
		int lastFps=0;
		Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
       /////////////////////////////////////THIS IS THE FLICKERING REPAINTING OUTSIDE CLIPREGION////////////////////////////
        GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, screenWidth, screenHeight, Color.RED);
        g.setPaint(gradient);
        g.fillRect(0,0,screenWidth,screenHeight)
       ////////////////////////////////////
		setFastRendering(g);
		while (gameRunning)
		{
			int delta = (int)(System.currentTimeMillis() - lastLoopTime);
			lastLoopTime = (int) System.currentTimeMillis();
			fpsCounter++;
			
			g = (Graphics2D) strategy.getDrawGraphics();
			g.clipRect(10,80,screenWidth-20,screenHeight-90);
			g.setColor(Color.black);
			g.fillRect(10,80,screenWidth-20,screenHeight-90);
			
			//TilesEntities moves
			
			//Background
			drawBackground(g);

			// collisions
			
			//Entities moves
			tank.move(delta);
			
			// drawing
			
			Entity entity = (Entity) entities.get(0);	
			entity.draw(g);
			lastFps=hud.displayFps(g,delta,lastFps,fpsCounter);
			// remove any entity that has been marked for clear up
			
			g.dispose();
			try { Thread.sleep(0); } catch (Exception e) {}
			strategy.show();
			//
			tank.setHorizontalMovement(0);
			scrollEntities(g,delta);
		}
	}


Here is what i double buffer :-o


	public Game()
	{
		// Determine if page flipping is supported
	    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    gd = ge.getDefaultScreenDevice();
	    GraphicsConfiguration gc = gd.getDefaultConfiguration();
	    BufferCapabilities bufCap = gc.getBufferCapabilities();
	    boolean page = bufCap.isPageFlipping();
		
		Frame container = new Frame(gd.getDefaultConfiguration());
		win = new Window(container);
		gd.setFullScreenWindow(win);
		win.setIgnoreRepaint(true);
		container.pack();
		container.setResizable(false);
		container.setVisible(true);
		container.setIgnoreRepaint(true);
		container.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		container.addKeyListener(new KeyInputHandler());
		win.addMouseListener(new MouseInputHandler());
		win.createBufferStrategy(2);
		strategy = win.getBufferStrategy();
		container.requestFocus();
		hud = new HUD();
		cursorChanger = new CursorChanger();
		cursorChanger.setCursor("default");
		iniBackground();
		startGame();
		gameLoop();
	}

As Anon666 said I don’t think its possible to do dirty (partial) repaints with a BufferStrategy (you could with a single-buffer strategy but there’s no point, it would be inefficient, and there would be flicker & artifacts). The reason you can’t do it is the bufferStrategy.show() method paints the complete back image, not a part of it. You can still do partial paints on the back-buffer and then show the whole thing, however. This is what you’re doing already and it may be sufficient. Performance-wise its worse than the below solution but since you’re using full-screen, on Windows you may be able to flip rather than blit the back buffers which is very quick.

If you really want to do partial/clipped paints then you will have to do away with the bufferStrategy and make your own VolatileImage back buffer which you can use to do a clipped drawImage to the screen. This is a bit of work though, so you may want to just continue as you are. I hope that helps,

Keith

::slight_smile: nice answer !
I will give up this idea and make no clip :-\ . (no time enough to try volatileImage now ::))
Anyway i think it will work not so bad without clipping.

edit : is there an efficient solution to clip images ?

Thanx for replying ! 8)