mgsLib  1.3
Mermaja's Graphic Screen. A simple C library to build Windows graphic applications from console programs.
figures.c

This example shows how to draw bordered and non bordered figures. Then it uses rotated figures in a simple animation. It has been used to generate the third image of the documentation.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include "mgsLib.h"
// Points to define the generic polygon (x and y coordinates for each point)
int points[] = {500, 300, 470, 250, 520, 255, 560, 280, 555, 320, 535, 345, 480, 340};
int main()
{
int scr;
int angles[5] = {0};
// Create a screen
if ((scr = mgsCreateScreen(800, 600, "Animating figures", 0xFFC0C0, NULL)) < 0)
exit(EXIT_FAILURE);
// Draw four figures near the corners
// A bordered rectangle top left
mgsBorderedRectangle(scr, 50, 50, 150, 130, MGS_CRED, 3, 0x202080);
// A five points star top right
mgsStarCR(scr, 5, 700, 90, 70, 30, MGS_CBLUE);
// A regular heptagon bottom left
mgsRegPolygonCR(scr, 7, 100, 510, 60, MGS_CBLUE);
// A bordered ellipse bottom right
mgsBorderedEllipse(scr, 700, 510, 40, 70, MGS_CRED, 3, 0x202080);
// Finally the 7 sides polygon whose vertex are defined in the points vector
mgsBorderedPolygon(scr, 7, points, 0x208020, 8, MGS_CBLACK);
// Show the created image
// Wait for A to be pressed
puts("Press A to animate");
while(!mgsKeyPressed('A'));
// And animate until S is pressed
puts("Press S to exit");
while(!mgsKeyPressed('S')) {
// We update the angles. The rectangle and ellipse will rotate counterclockwise
// around their respective centers; the star and regular polygon clockwise.
angles[0] += 2;
angles[1] -= 2;
angles[2] -= 2;
angles[3] += 2;
// And the generic polygon slower, counterclockwise and around the center of the screen
angles[4] += 1;
// Clear the previous screen
// Draw the rotated figures
mgsRotatedBorderedRectangle(scr, 50, 50, 150, 130, angles[0], MGS_CRED, 3, 0x202080);
mgsRotatedStarCR(scr, 5, 700, 90, 70, 30, angles[1], MGS_CBLUE);
mgsRotatedRegPolygonCR(scr, 7, 100, 510, 60, angles[2], MGS_CBLUE);
mgsRotatedBorderedEllipse(scr, 700, 510, 40, 70, angles[3], MGS_CRED, 3, 0x202080);
mgsRotatedBorderedPolygon(scr, 7, points, 400, 300, angles[4], 0x208020, 8, MGS_CBLACK);
// Wait until the frame time expires
// And show the new image frame
}
}