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.
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 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.
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.
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.
#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
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/
// 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.
constfloat 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();
}
}