Advertisement Jump to content
Sign in to follow this  

Tearing graphics in ClanLib

This topic is 4979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

// changed it to use random shades of red each frame
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
	virtual int main(int argc, char **argv)
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		quit = false;

			// Initialize ClanLib base components

			// Initialize the ClanLib display component

			// Initilize the OpenGL drivers

			// 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

				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

				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));


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

				// This call updates input and performs other "housekeeping"
				// call this each frame

			// De-Initialize the ClanLib GL component

			// De-Initialize the ClanLib display component

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

			// Display console close message and wait for a key

			return -1;

		return 0;

	bool quit;

	void on_input_up(const CL_InputEvent &key)
		if( == 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?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!