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

This example shows how to use selection lists. It presents a strange, moving list of options to select the color of the background and a normal, rectangular section list to control the movement of the first. It also adds a button to end the execution of the program.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mgsLib.h>
#include <mrmWidgets.h>
#define SCRW 800
#define SCRH 600
#define RAD 250.0
#define SPEED 0.005
void setPos(double angle);
int setCol(int op);
int contrast(int c);
// Texts for both selection lists
char *modeText[3] = {"Rotate counterclockwise", "Stop rotation", "Rotate clockwise"};
char *colorText[6] = {"Red", "Magenta", "Blue", "Yellow", "Green", "Cyan"};
// Colors associated with teh second one
int colors[6] = {MGS_CRED, MGS_CMAGENTA, MGS_CBLUE, MGS_CYELLOW, MGS_CGREEN, MGS_CCYAN};
// And positions.
int hexa[12];
int main() {
// Handlers for the main graphic elements
int scr, sc, sc2, btn;
// Options from the selections and colors
int op, op2 = 0, oldop2 = 1, col, col2;
// Animation variables
double a = 0.0, v = SPEED;
// Create the main screen and set the font for its texts
scr = mgsCreateScreen(SCRW, SCRH, "MrMWidget Test", MGS_CBLACK, NULL);
mgsSetFont(scr, 18, MGS_CWHITE, MGS_TRANSPARENT);
// Move the main window to a suitable place on screen
mgsMoveScreen(scr, 300, 150);
// Create the first, static selection and set its parameters
sc = mrmSelectionInit(scr, 200, 75, 3, modeText, 1, MWG_SCUNIQUE | MWG_SCROUND | 10);
mrmSelectionSetFont(sc, 20, 0, "Arial", MGS_BOLD);
mrmSelectionSetFontColor(sc, 0xF06060, 0xFF1010, 0, 0);
mrmSelectionSetPos(sc, 300, 260);
// Create the moving selection and set only its font.
sc2 = mrmSelectionInit(scr, 0, 0, 6, colorText, 0, MWG_SCMULTI | MWG_SCSYMBOL | 'V');
mrmSelectionSetFont(sc2, 25, 0, "Forte", MGS_BOLD);
// Create the exit button and set its aspect
btn = mrmButtonInit(scr, 80, 40, NULL, "Exit", MGS_CGREY, 0x202020, MGS_CBLACK);
mrmButtonSetFont(btn, 26, 0, "Arial", MGS_BOLD);
mrmButtonSetFontColor(btn, 0x2020E0, MGS_CRED, 0x2020E0);
mrmButtonSetPos(btn, SCRW - 100, SCRH - 50);
// Main animation loop: stay until the user presses the button.
while(mrmButtonGetCounter(btn) == 0) {
// Calculate the vertex of a hexagon to place the options
// rotated an angle a.
setPos(a);
// Assign the positions to the selection
// Increase angle according to speed
a = a + v;
// Man widget loop. Selections values will change here
// Calculate speed according to the option selected on the
// static list. As its mode is unique, a number between 0 and 2
// identifies the selected option. Use it to set speed.
v = (1 - op) * SPEED;
// Read the option selected on the moving list
op2 = mrmSelectionGetVal(sc2);
// Calculate new aspects only if the selection has changed
if (op2 != oldop2) {
oldop2 = op2;
// Calculate color for the background according to the selected option
col = setCol(op2);
// And a contrasted color for the text -it can be invissible if fixed!
col2 = contrast(col);
mrmSelectionSetFontColor(sc2, col2, col2, 0, 0);
}
// Prepare to draw new frame
// Two rectangles first, full screen and for the static list
mgsRectangle(scr, 0, 0, SCRW - 1, SCRH - 1, col);
mgsRectangle(scr, 280, 240, 530, 345, 0xB0B0B0);
// Draw the widgets on the rectangles.
// And synchronize and update screen to end the animation.
}
// Good bye.
exit(EXIT_SUCCESS);
}
// This funcion just calculates the coordinates of the 6 vertex of
// an hexagon of radius RAD, rotated an angle a
void setPos(double angle)
{
int i;
for (i = 0; i < 6; i++) {
hexa[2 * i] = SCRW / 2 - 50 + RAD * cos(angle + i * 2.0 * M_PI / 6.0); //x
hexa[2 * i + 1] = SCRH / 2 - RAD * sin(angle + i * 2.0 * M_PI / 6.0); // y
}
}
// Calculates the background color by adding the selected colors, dividing by 2
// each component -than can only be 0 or FF- and saturating the resulting value
// to a byte size.
// As op comes from a multiple selection, it is a bitmap with the order of the bit
// indexing the option and its value meaning selected or not.
int setCol(int op)
{
int i, msc = 1, resR = 0, resG = 0, resB = 0;
for (i = 0; i < 6; i++) {
if (op & msc) {
resR += colors[i] & 0x7F;
resG += (colors[i] >> 8) & 0x7F;
resB += (colors[i] >> 16) & 0x7F;
}
msc = msc << 1;
}
resR = resR & 0xFF;
resG = resG & 0xFF;
resB = resB & 0xFF;
return mgsRGBColor(resR, resG, resB);
}
// Add 80 and saturate each component to get a contrasted color and avoid
// the text disappearing due to changes on the background color.
int contrast(int c)
{
int r, g, b;
r = ((c & 0xFF) + 0x80) & 0xFF;
g = (((c >> 8)& 0xFF) + 0x80) & 0xFF;
b = (((c >> 16)& 0xFF) + 0x80) & 0xFF;
return mgsRGBColor(r, g, b);
}