Is it possible to output graphics without using windows.h

Pages: 12
I use Codeblocks and TC 3.0.
I use TC's textcolor and textbackground functions to output colourful text. But only 16 colors are possible.
Also I tried creating circles and rectangles by TC's graphics.h in DOSBox, but an error occurs 'EGAVGA.BGI library missing' even if it exists.
Please help
You probably do not want to write programs for DOS... so doing anything with DOSBox or graphics.h is ill advised.

You also do not want to do anything with EGA/VGA as those are not used anymore.


Get a graphic lib. If you're interested in drawing graphics and not so interested about drawing widgets/controls (like push buttons, checkboxes, etc), then libs like SFML and SDL will work.

If you want widgets, then widgetry libs like wxWidgets and Qt will work... though be warned they are much more complicated.
I second SFML, a bunch of us here have at least some experience with it on a multitude of platforms and different IDE's. Also the tutorial won't take you more then a week to get through, and that's if you're really thorough. As a final selling point it's not strictly a "gaming" library, it's generic enough that I've thought about using it in some apps I write for my office.
Ok I'll try sfml. Thank you for your help.
I read about SFML. It can do a lot than i need. Isn't there anything that will just draw a simple rectangle ?
If you're looking for basic, quick-n-easy shape drawing, C++ probably isn't the right language. Any graphic lib worth using is going to require you to set up and manage some kind of window. It really doesn't get much simpler than what SFML offers.
I don't know about SDL 2.0, but with SDL 1.2, you can easily just use SDL_FillRect, and it creates a rectangle at the desired position.
SDL 1.2 also requires you create a window and have a basic event loop.

It's no simpler than it is in SFML.
I know. I didn't know if SFML had a rectangle drawing functionality.

Anyway, I'm clueless on why he only wants a lib that draws basic shapes.
I thought there was something simpler than that and it wasn't good to rely on a library always. So I'll just drop the idea and start learning sfml. Thank you.
Last edited on
You could draw with Win32, or OpenGL. That is not simple though. SFML is simply a wrapper... that means SFML handles Win32 for you and gives you a simpler interface.
Try Allegro, albeit it can be a pain to set up for some predicaments.

http://alleg.sourceforge.net/stabledocs/en/alleg013.html#rectfill

It is very simple to initialize (less than ten lines of code), and drawing a rectangle is a sinch.

You can also develop complete games with the library as well, using all of its features.

PS: If you want to avoid the OS API to interface with the video driver, a "wrapper" (http://en.wikipedia.org/wiki/Wrapper_library) will be perfect for you, and for a beginner SDL/Allegro are ideal.

closed account (3qX21hU5)
Code that draws a simple rectangle in SFML 2.1 with some other properties like position and rotation.

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
#include <SFML/Graphics.hpp>

int main()
{
	sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
	sf::RectangleShape shape(sf::Vector2f(200, 100));

	shape.setFillColor(sf::Color::Blue);  // Set the color fill

	// You can ignore this it is just setting the origin to the center of the rectangle.
	// This makes it easier to place the rectangle on screen
	shape.setOrigin(shape.getSize().x / 2, shape.getSize().y / 2);

	shape.setPosition(window.getSize().x / 2, window.getSize().y / 2);	// Set the position to the middle

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		// Now lets rotate the rectangle every frame. Now I should note this is not a good way to do this.
		//  You will want to use a time step instead for your update calls but that is another topic
		// and didn't want to get into all that so just used this. It rotates the shape by .01 degrees
		// every frame (Remember there can be hundreds of frames per second and it varies by computer speed).
		shape.rotate(.01);

		window.clear();
		window.draw(shape);
		window.display();
	}

	return 0;
}


It shows the basic idea behind it and some of the things you can do with the shapes. If you want to know more about SFML and shapes check out their tutorial specifically this one http://www.sfml-dev.org/tutorials/2.1/graphics-shape.php
Last edited on
Is that directly able to be compiled?
Why not try before asking?
Because I don't have SFML installed, and it's more trouble than its worth to get it working for one example.
It should be compilable, and yes, that's how simple it really is.
closed account (3qX21hU5)
Is that directly able to be compiled?


Yes it will run as long as you have SFML 2.X linked to the project. All it shows is a window with a black background and a blue rectangle rotating in the middle of the screen.

Though would just like to point out again that the "hack" that I used for the rotation update should not be used in your actual projects. Instead you should have a timestep that you can use to calculate how much rotation to give for that given frame depending on the delta time.

For example you might see something like this. Not much comments on this explaining the timestep just was meant as a example of what it might look like. If you want more info on timesteps here is a great article on them http://gafferongames.com/game-physics/fix-your-timestep/

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
// This is how many frames per second we would like.
const sf::Time TimePerFrame = sf::seconds(1.0f / 60.0f);

// How fast our rotation speed should be.
const float rotationSpeed = 90.0f;

int main()
{
	// Declare our window to work with.
	sf::RenderWindow window(sf::VideoMode(800, 600), "Time Steps");

	// Setup your rectangle shape.
	sf::RectangleShape rectangle(sf::Vector2f(200, 100));
	rectangle.setOrigin(rectangle.getSize().x / 2, rectangle.getSize().y / 2);
	rectangle.setPosition(window.getSize().x / 2, window.getSize().y / 2);
	rectangle.setFillColor(sf::Color::Blue);

	// We will use this to keep track of the time that has past each frame.
	sf::Clock clock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (window.isOpen())
	{
		sf::Time deltaTime = clock.restart();
		timeSinceLastUpdate += deltaTime;
		while (timeSinceLastUpdate > TimePerFrame)
		{
			timeSinceLastUpdate -= deltaTime;

			sf::Event event;
			while (window.pollEvent(event))
			{

				if (event.type == sf::Event::Closed)
					window.close();
			}	

			// We get real the real-time state of the keyboard to determine what buttons
			// might be pressed then act accordingly
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
				rectangle.rotate(-rotationSpeed * deltaTime.asSeconds());

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
				rectangle.rotate(rotationSpeed * deltaTime.asSeconds());

		}

		window.clear();
		window.draw(rectangle);
		window.display();
	}
}
Last edited on
What about ASCII?
Pages: 12