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

This example shows how to use the mouse input functions to detect if the user clicks on any of several rectangles.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include "mgsLib.h"
#define WIDTH 100
#define HEIGHT 80
#define INTER 50
//Left, top, right and bottom coordinates
struct rectangle {
int l, t, r, b;
};
// Prototype of the function
inline int inRectangle(int x, int y, struct rectangle r);
int main()
{
// Colors for the rectangles and other variables
int i, scr, colors[5] = {MGS_CRED, MGS_CMAGENTA, MGS_CGREEN, MGS_CCYAN, MGS_CBLUE};
int x, y, selected = -1, pressed = 0;
struct rectangle rect[5];
// Create a screen
if ((scr = mgsCreateScreen(800, 600, "Mouse input", MGS_CGREY, NULL)) < 0)
exit(EXIT_FAILURE);
// Align the rectangles horizontally
for(i = 0; i < 5; i++) {
rect[i].t = 180;
rect[i].b = 180 + HEIGHT;
rect[i].l = INTER + i * (INTER + WIDTH);
rect[i].r = rect[i].l + WIDTH;
}
// Set the default font
mgsSetFont(scr, 80, MGS_CBLACK, MGS_TRANSPARENT);
// And work until S is pressed
puts("Press S to exit");
while(!mgsKeyPressed('S')) {
// Always read the mouse position. It does not cost much time
x = mgsMouseX(scr);
y = mgsMouseY(scr);
// pressed is the state of the left mouse button.
// If it was pressed the previous frame
if (pressed) {
// See if it has ben released
if (!(mgsMouseButtons(scr) & MGS_MOUSELEFT)) {
// It is no longer pressed
pressed = 0;
// And no rectangle is selected for sure
selected = -1;
}
// Else if we have any rectangle selected
else if (selected > -1)
// If the cursor has left its surface, deselect it
if (!inRectangle(x, y, rect[selected]))
selected = -1;
}
// If it was not pressed verifiy if it is pressed now
else if (mgsMouseButtons(scr) & MGS_MOUSELEFT) {
// If so, mark it as pressed
pressed = 1;
// And if over a rectangle, select it
for (i = 0; i < 5; i++)
if (inRectangle(x, y, rect[i])) {
selected = i;
break;
}
}
// Clear the old screen
// Put the main text in the default font
mgsPuts(scr, 37, 20, "Click on any rectangle");
// Draw all the rectangles. If any is selected use a color darker than the default
for(i = 0; i < 5; i++)
mgsRectangle(scr, rect[i].l, rect[i].t, rect[i].r, rect[i].b, (i == selected) ? colors[i] & 0x7F7F7F : colors[i]);
// If any rectangle is selected, display an effect keeping the default font
if (selected > -1) {
mgsSetFont(scr, 70, colors[selected], MGS_TRANSPARENT);
mgsPuts(scr, 130, 370, "You clicked on the");
mgsPuts(scr, 90, 440, "rectangle of this color");
mgsRectangle(scr, 10, 520, 790, 540, colors[selected] & 0x7F7F7F);
mgsRectangle(scr, 10, 350, 790, 370, colors[selected] & 0x7F7F7F);
mgsSetFont(scr, 80, MGS_CBLACK, MGS_TRANSPARENT);
}
// Wait until the frame time expires
// And show the new image frame
}
exit(EXIT_SUCCESS);
}
// Just verify (x,y) are inside the rectangle.
inline int inRectangle(int x, int y, struct rectangle r)
{
return((x >= r.l) && (x <= r.r) && (y >= r.t) && (y <= r.b));
}