Tearing graphics in ClanLib

Started by
-1 comments, last by Necrosis 18 years, 10 months ago
Well, I'm trying to learn Clanlib 2d graphics and making a game in the process so I have a nice reference of how I used the various features of the library in the future. Now, I'm basing my graphics off the Basic2D example provided in the library which I'll provide the code for. Now the graphics drawn dont tear at all, everything draws smooth and fine, and they do in my game as well. However, one of the features in my game is a flashing background in certain occasions (where the whole background flashes between yellow and blue very fast for a seizure inducing effect) and seemingly no matter what method I try I keep getting tearing ONLY in the background. First thing I tried was this

// this is the original code
CL_Display::clear(CL_Color(0,0,56));

// changed it to use random shades of red each frame
CL_Display::clear(CL_Color(rand()%255,0,0));
However, this of course caused serious tearing wherever the color was being drawn... while the rest of the graphics looked fine. Next idea was to just make two sprite images and draw them in place of the clear screen thing, with a counter variable it switched between drawing one then the other so I could make it flash at slower intervals. However, I got tearing with that too, using my counter to slow the rate down the tearing did seem to go down a bit but it was still there. While this in itself isnt a big problem for the current project, I'm worried that I might get this kind of effect in future projects where I'll be using animated sprites and stuff. I cant really find any reliable tutorials and I'm not the best with reading documentation, but the example was fairly self explanatory and there is a CL_Display::flip(); in the code so I'm under the assumption it is double buffered. So I must be missing something that I havent been able to figure out in the documentation. What am I doing wrong? Basic 2D example

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>

class ExampleBasic2D : public CL_ClanApplication
{
public:
	virtual int main(int argc, char **argv)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		console.redirect_stdio();
		
		quit = false;

		try
		{
			// Initialize ClanLib base components
			CL_SetupCore::init();

			// Initialize the ClanLib display component
			CL_SetupDisplay::init();

			// Initilize the OpenGL drivers
			CL_SetupGL::init();

			// Set a videomode - 640x480
			CL_DisplayWindow window("ClanLib Basic2D Example", 640, 480);

			// Connect the Window close event
			CL_Slot slot_quit = window.sig_window_close().connect(this, &ExampleBasic2D::on_window_close);

			// Connect a keyboard handler to on_key_up()
			CL_Slot slot_input_up = (window.get_ic()->get_keyboard()).sig_key_up().connect(this, &ExampleBasic2D::on_input_up);

			// Load a surface from a targa-file
			CL_Surface sur_logo("logo.tga");

			double sin_count = 0.0f;
			float ypos = 0.0f;
			float ydir = 3.0f;

			// Run until someone presses escape
			while (!quit)
			{
				// Clear the display in a dark blue nuance
				// The four arguments are red, green, blue and alpha
				// All color nuances in ClanLib are measured in the interval 0->255
				CL_Display::clear(CL_Color(0,0,56));

				ypos += ydir;
				if (ypos+200 >= CL_Display::get_height() || ypos < 0)
				{
					ydir *= -1;
				}
				
				CL_Display::draw_line(0, (int)(ypos-1), CL_Display::get_width(), (int)(ypos-1),CL_Color(127, 0, 0));
				CL_Display::draw_line(0, (int)(ypos+198), CL_Display::get_width(), (int)(ypos+198), CL_Color( 127, 0, 0));

				// Show the logo surface.
				// Use the get_width() and get_height() functions of both
				// CL_Display and CL_Surface, to show the surface in the bottom right corner
				sur_logo.draw(
					CL_Display::get_width()-sur_logo.get_width(),
					CL_Display::get_height()-sur_logo.get_height(),
					window.get_gc());

				CL_Display::push_cliprect(CL_Rect(0, (int)(ypos), CL_Display::get_width(), (int)(ypos+198)));

				// Draw a rectangle in the center of the screen
				// going from (240, 140) -> (440, 340) _not_ including the 
				// pixels in the right-most column and bottom-most row (440, 340)
				CL_Display::fill_rect(CL_Rect(240, 140, 440, 340), CL_Color(255,255,255,255));

				// Frame the rectangle with red lines
				CL_Display::draw_line(240, 140, 440, 140, CL_Color(255, 0, 0));
				CL_Display::draw_line(240, 340, 440, 340, CL_Color(255, 0, 0));
				CL_Display::draw_line(240, 140, 240, 340, CL_Color(255, 0, 0));
				CL_Display::draw_line(440, 140, 440, 340, CL_Color(255, 0, 0));

				// Show a few alpha-blending moving rectangles that moves in circles
				float x = float(cos(sin_count)*120);
				float y = float(sin(sin_count)*120);
				sin_count += 0.05f;
				CL_Display::fill_rect(CL_Rect((int)(320+x-30), (int)(240+y-30), (int)(320+x+30), (int)(240+y+30)), CL_Color(0, 255, 0, 127));
				x = float(cos(sin_count+3.14159)*120);
				y = float(sin(sin_count+3.14159)*120);
				CL_Display::fill_rect(CL_Rect((int)(320+x-30),(int)(240+y-30),(int)(320+x+30),(int)(240+y+30)), CL_Color(255, 255, 0, 127));

				CL_Display::pop_cliprect();

				// Flip the display, showing on the screen what we have drawed
				// since last call to flip_display()
				CL_Display::flip();

				// This call updates input and performs other "housekeeping"
				// call this each frame
				CL_System::keep_alive(10);
			}

			// De-Initialize the ClanLib GL component
			CL_SetupGL::deinit();

			// De-Initialize the ClanLib display component
			CL_SetupDisplay::deinit();

			// De-Initialize ClanLib base components
			CL_SetupCore::deinit();
		}
		catch(CL_Error error)
		{
			std::cout << "Exception caught : " << error.message.c_str() << std::endl;			

			// Display console close message and wait for a key
			console.display_close_message();

			return -1;
		}

		return 0;
	}

private:
	bool quit;

	void on_input_up(const CL_InputEvent &key)
	{
		if(key.id == CL_KEY_ESCAPE)
			quit = true;
	}

	void on_window_close()
	{
		quit = true;
	}
} my_app;


Oh one more question while I'm at it, I'm under the assumption this code is 3d accelerated through OpenGL, I cant find a straight answer from the documentation, but it does require including that GL header, am I correct in assuming it is hardware accelerated in 3d?

This topic is closed to new replies.

Advertisement