How to create USER DEFINED EVENTS in ALLEGRO 5 - c

I am working on a game in allegro 5 in which I want to create rectangular objects dynamically on screen and make them clickable with mouse buttons
al_register_event_source( event_queue, al_get_timer_event_source(timer));
al_register_event_source( event_queue, al_get_mouse_event_source());
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
al_start_timer(timer);
while ( !exit )
{
ALLEGRO_EVENT ev;
al_wait_for_event( event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
;
else if ( ev.type == ALLEGRO_EVENT_MOUSE_AXES )
{
x = ev.mouse.x;
y = ev.mouse.y;
}
else if ( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
{
if ( x >= rect.x && x <= rect.maxx && y >= rect.y && y <= rect.maxy )
destory ( rect );
}
else if ( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
break;
if ( redraw && al_event_queue_is_empty(event_queue)){
redraw = false;
al_draw_rectangle ( rect.x, rect.y, rect.maxx, rect.maxy, blue, 1 );
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
But this is hardcoded for only one rectangle. How can I make an event for this which can handle rectangles like button.

User events aren't needed to respond to button presses.
Rather, you should approach this from a different angle. Pass the ALLEGRO_EVENT to your button class and have it return whether it was clicked or not.
bool Button::ButtonPressed(ALLEGRO_EVENT ev) {
if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button == 1) {
if (our_area.Contains(ev.mouse.x , ev.mouse.y)) {return true;}
}
return false;
}
BUT, you asked how to create user events in Allegro 5 so I will answer that as well.
See https://liballeg.org/a5docs/trunk/events.html#allegro_user_event for details and a code example. Basically, you create and initialize an ALLEGRO_EVENT_SOURCE, register it with your event queue, and then listen for messages you emit with al_emit_user_event.

Related

Arduino with adafruit RGBLCDShield Buttons weird behavior

I have the Arduino uno with rgb lcd shield. there is a very weird behavior with the buttons in one specific function.
The function is called yes/no. It displays a message on the screen (working) the user can select answer yes/no with the buttons up/down/left/right and approve the answer by pressing select button.
The function is as follows:
bool yesno(String message)
{
//Serial.println("asking yesno question " + message);
bool answer = false;
bool answerSelected = false;
setColor('r');
setText(message+'?', "no");
while (!answerSelected)
{
uint8_t buttons = lcd.readButtons();
if (buttons){
if (buttons &(BUTTON_UP || BUTTON_DOWN || BUTTON_RIGHT || BUTTON_LEFT)) {
if (answer) {
answer = false;
setColor('r');
setText(message+'?', "no");
Serial.println(answer);
}
else {
answer = true;
setColor('g');
setText(message+'?', "yes");
Serial.println(answer);
}
}
else if (buttons & BUTTON_SELECT) {
setColor('w');
answerSelected = true;
Serial.println("selected ");
Serial.println(answer);
return answer;
}
}
delay(50);
}
}
for some reason, when pressing left/right/up/down, nothing happens. When pressing select, instead it executes function up/down/left/right
using if (buttons &&(BUTTON_UP || BUTTON_DOWN || BUTTON_RIGHT || BUTTON_LEFT)) instead, the buttons left/right/up/down work as intended but the button select still acts as the other buttons
similar code for a menu works as intended:
void InitializeMenu()
{
// initialize
Serial.println("entering menu");
setColor('w');
while (!exitMenu)
{
if (menuItem != selectedItem)
{
Serial.println("switching menu to: "+menuItems[menuItem]);
setText("Menue", menuItems[menuItem]);
selectedItem = menuItem;
}
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
menuItem--;
Serial.println("menu up");
Serial.println(menuItem);
Serial.println(menuSize);
if (menuItem < 0)
{
menuItem = menuSize;
Serial.println("start of menu going to end");
}
}
if (buttons & BUTTON_DOWN) {
menuItem++;
Serial.println("menu down");
Serial.println(menuItem);
Serial.println(menuSize);
if (menuItem > menuSize) {
menuItem = 0;
Serial.println("end of menu going to start");
}
}
if (buttons & BUTTON_SELECT) {
Serial.println("enter");
if (menuItem == menuSize) exitMenu = true;
}
delay(50);
}
}
I want to keep the code as short and simple as possible due to very limited space on the arduino uno.
BUTTON_UP || BUTTON_DOWN || BUTTON_RIGHT || BUTTON_LEFT
doesn't do what you expect it to do. || is logical or, and the entire expression evaluates to true. To get a bitmask, change it to bitwise or (|):
BUTTON_UP | BUTTON_DOWN | BUTTON_RIGHT | BUTTON_LEFT

Why is SDL_RenderCopy so long?

I'm creating a game with SDL2 in C and I have a problem, I'm actually doing the main menu of my game: I have 3 buttons on my menu and I want them to go red when I put my mouse over them, so what I did is I pre-loaded 4 SDL_Textures for my entire screen: one with all buttons not colored and the 3 others are the same main menu with each button highlighted in red, I thought it would be faster than changing the button color and recreating the texture each time, but when I put my mouse over the buttons they are passing red really slow, I'm just using SDL_RenderCopy and SDL_RenderPresent each time the screen is updated so it shouldn't be that long. Here's my function code and a picture of my program to understand better. (sorry for the bad english btw)
typedef struct s_sdlVar
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
SDL_Texture *mainMenu[MAIN_MENU_TEXTURE_COUNT];
t_rect mainMenuButtonRect[MAIN_MENU_OPTION_COUNT];
t_res screenResolution;
int boxSiz;
t_byte difficulty;
} t_sdlVar;
static int checkButtonPos(t_rect *buttonPos, int x, int y)
{
for (int i = 0; i < MAIN_MENU_OPTION_COUNT; i++)
if ((x >= buttonPos[i].x) && (x < buttonPos[i].x + buttonPos[i].w)
&& (y >= buttonPos[i].y) && (y < buttonPos[i].y + buttonPos[i].h))
return (i + 1);
return (0);
}
int displayMainMenu(t_sdlVar *sdl)
{
t_bool exit = false;
t_bool isButtonOn = false;
int textureIndex = 0, buttonIndex = 0;
if (SDL_RenderCopy(sdl->renderer, sdl->mainMenu[0], NULL, NULL))
return (-1);
SDL_RenderPresent(sdl->renderer);
while (!exit)
if (SDL_PollEvent(&sdl->event))
{
if (sdl->event.type == SDL_MOUSEMOTION)
{
if ((textureIndex = checkButtonPos(sdl->mainMenuButtonRect, sdl->event.motion.x, sdl->event.motion.y)))
{
if (SDL_RenderCopy(sdl->renderer, sdl->mainMenu[textureIndex], NULL, NULL))
return (-1);
SDL_RenderPresent(sdl->renderer);
isButtonOn = true;
}
else if (isButtonOn)
{
if (SDL_RenderCopy(sdl->renderer, sdl->mainMenu[0], NULL, NULL))
return (-1);
SDL_RenderPresent(sdl->renderer);
isButtonOn = false;
}
}
else if ((sdl->event.type == SDL_QUIT) || ((sdl->event.type == SDL_MOUSEBUTTONDOWN) && isButtonOn && (textureIndex == 2)))
exit = true;
}
return (buttonIndex);
}

D3 import .csv to 2Darray & opacity change when hover

I'm currently writing a visualization via D3 and I hope you can help me out.
To get an idea of what I'm making you can view this link: https://dl.dropboxusercontent.com/u/56480311/Data-visualization/index.html
I'm facing two problems: the first is that I can't seem to place .csv data in a 2D array. I found multiplie tutorials about placing it in a normal array but the only solution I came up with if your data is in a table, is to use a 2D array. I couldn't find how to do this so what I thought would help:
var lines = []; //define lines as an array
//retrieve data from .csv file
d3.csv("https://dl.dropboxusercontent.com/u/56480311/Data-visualization/growth-prep.csv", function(data) {
lines = data.map(function(d) {
return d;
});
});
//create 2D array named myArray
var myArray = new Array(10);
for (var i = 0; i < 10; i++) {
myArray[i] = new Array(18);
}
//place each value of .csv file into myArray
for (var j = 0; j < lines.length; j++) {
var values = lines[j].split(";");
for (var k = 0; k < values.length; k++) {
myArray[k][j] = values[k];
}
}
This code doesn't work, however. Console keeps saying that the arrays are undefined and they're empty.
A second problem I'm facing is in visualizing the data. I sum three values that are next to each other (so for example dataset[0][0] + dataset[0][1] + dataset[0][2]). I draw this value as the width of a rectangle. On top of that, I want to draw three different rectangles each with the value of one of three. So in this case there is one rectangle consisting of dataset[0][0] + dataset[0][1] + dataset[0][2] and on top there is a rectangle showing data dataset[0][0], one showing data dataset[0][1] and a third showing data dataset[0][2]. I want the three smaller ones only to appear once the mouse hovers over the 'sum' / parent rectangle.
If you view the link you can see what I mean. Everything is already there, only the three smaller rectangles have opacity 0 so you don't see them yet (but you can find them via inspect elements).
I figured I could do this by setting the opacity of the three rectangles to 0 and to 1 once the mouse hovers. The best solution to me would be to place the three rectangles into one kind of div. Once the mouse hovers over it, its opacity is 1. Only I can't figure out how to easily give different class names without having to do it by hand for each rectangle separately. Does anyone know how to do this or else have a better solution to change the opacity of all three rectangles once the mouse hovers over the 'bigger' rectangle?
This is the code:
var dataset = [[6,6,3,3,3,0,6,6,0,12,6,6,0,0,18,6,3,3],[3,0,0,6,3,0,3,3,0,9,3,0,0,0,18,6,6,6],[3,0,3,6,3,3,6,0,3,9,6,3,0,0,15,6,6,6],[6,6,3,3,0,3,6,6,6,12,6,6,0,0,18,6,6,3],[6,6,0,6,0,0,6,6,6,12,6,6,0,0,24,6,6,0],[3,6,3,6,3,3,6,6,3,3,0,0,0,0,15,3,9,6],[3,3,0,3,0,3,3,3,3,9,3,0,0,0,15,6,3,9],[0,0,0,3,0,6,6,3,3,3,0,3,0,0,24,6,12,6],[6,6,3,6,0,0,9,9,6,12,6,6,0,0,15,3,3,3],[6,6,0,6,3,0,6,6,0,9,6,3,0,0,9,3,6,0]];
var h = 800;
var w = 800;
//create svg of 800x800px
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var xPos = 150;
for (var k = 0; k < 10; k++) { //loop through lines of array
var yPos = 40 + k*80;
for (var j = 0; j < 18; j++) { //loop through rows of array
var count = j + 1;
//assign data of three boxes to value1/value2/value3 with which we will later on draw three separate rectangles to visualize the data
if (count == 1 || count == 4 || count == 7 || count == 10 || count == 13 || count == 16) {
var value1 = dataset[k][j];
}
if (count == 2 || count == 5 || count == 8 || count == 11 || count == 14 || count == 17) {
var value2 = dataset[k][j];
}
if (count == 3 || count == 6 || count == 9 || count == 12 || count == 15 || count == 18) {
var value3 = dataset[k][j];
}
if (count % 3 ==0) {
var sum = dataset[k][j] + dataset[k][j-1] + dataset[k][j-2]; //count the three values to also draw one bigger rectangle of their sum
var rectangle = svg.append("rect")
.attr("x", xPos)
.attr("y", yPos)
.attr("width", sum*5)
.attr("height", 20)
.attr("fill", function(d) {
if (count == 3) {
return "LightSeaGreen";
}
else if (count == 6) {
return "MediumSeaGreen";
}
else if (count == 9) {
return "MediumSpringGreen";
}
else if (count == 12) {
return "LimeGreen";
}
else if (count == 15) {
return "ForestGreen"
}
else if (count == 18) {
return "GreenYellow"
}
});
for (var l = 0; l < 3; l++) { //draw three 'sub' rectangles on top of the one 'sum' rectangle. they should appear when the mouse hovers over their 'sum' rectangle
var rectangle2 = svg.append("rect")
.attr("class", "sqr")
.attr("x", function() {
if (l == 0) {
return xPos;
} else if (l == 1) {
return xPos + value1*5;
} else if (l == 2) {
return xPos + (value1+value2)*5;
}
})
.attr("y", yPos)
.attr("width", function() {
if (l == 0) {
return value1*5;
} else if (l == 1) {
return value2*5;
} else if (l == 2) {
return value3*5;
}
})
.attr("height", 20)
.attr("fill", function() {
if (l == 0) {
return "SteelBlue";
} else if (l == 1) {
return "aquamarine";
} else if (l == 2) {
return "SkyBlue";
}
})
.attr("opacity", 0); //in first instance the rectangles are not visible. Opacity should turn to 1 when the mouse hovers over their according div
}
if (sum > 0) {
xPos = xPos + sum*5 + 5;
}
}
}
xPos = 150;
}

moving objects withe key - diagonal

I need to move an object, I can move it up down and sideways, but fails in a diagonal, it is the way I made my move.
if (e.Key == Key.Up && Canvas.GetTop(Good) > 31)
{
double top = Canvas.GetTop(Good);
Canvas.SetTop(Good, top - 7);
}
if (e.Key == Key.Down && Canvas.GetTop(Good) < CanvasA.ActualHeight - 7 - Good.Height)
{
double down = Canvas.GetTop(Good);
Canvas.SetTop(Good, down + 7);
}
if (e.Key == Key.Left && Canvas.GetLeft(Good) > -2)
{
double left = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, left - 7);
}
if (e.Key == Key.Right && Canvas.GetLeft(Good) < CanvasA.ActualWidth - Good.Width)
{
double right = Canvas.GetLeft(Good);
Canvas.SetLeft(Good, right + 7);
}
}
}
Everything works but can not click on the two buttons to move the character in a diagonal,
The reason that there is less than 31, it's because I did Menu in WPF.

WinForms TreeView with both radios and checkboxes

I have a case where I would like TreeView to be able to show radio buttons on multiple root nodes, and checkboxes on their children. There would only be one level of children beneath any root node.
The radios should also behave like a group, ie one root is selected and the others' radios deselect.
I've been trying to fake it with images, but it doesn't look realistic. I originally had a listbox and a separate checkedlistbox, but the usability gods struck it down.
Has anyone implemented this functionality or have another suggestion?
Think of it this way:
(o) McDonalds
[ ] Burger
[ ] Fries
[ ] Drink
(o) Burger King
[ ] Burger
[ ] Fries
[ ] Drink
(*) Wendy's
[x] Burger
[x] Fries
[ ] Drink
You can have one big option, but select 1..n underneath the big option.
I came up with a solution based on the article http://www.codeproject.com/KB/combobox/RadioListBoxDotNetVersion.aspx. My implementation inherits from CheckedListBox and includes the following methods:
protected override void OnDrawItem (DrawItemEventArgs e)
{
// Erase all background if control has no items
if (e.Index < 0 || e.Index > this.Items.Count - 1)
{
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
return;
}
// Calculate bounds for background, if last item paint up to bottom of control
Rectangle rectBack = e.Bounds;
if (e.Index == this.Items.Count - 1)
rectBack.Height = this.ClientRectangle.Top + this.ClientRectangle.Height - e.Bounds.Top;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), rectBack);
// Determines text color/brush
Brush brushText = SystemBrushes.FromSystemColor(this.ForeColor);
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
brushText = SystemBrushes.GrayText;
Boolean bIsChecked = this.GetItemChecked(e.Index);
String strText;
if (!string.IsNullOrEmpty(DisplayMember)) // Bound Datatable? Then show the column written in Displaymember
strText = ((System.Data.DataRowView) this.Items[e.Index])[this.DisplayMember].ToString();
else
strText = this.Items[e.Index].ToString();
Size sizeGlyph;
Point ptGlyph;
if (this.GetItemType(e.Index) == ItemType.Radio)
{
RadioButtonState stateRadio = bIsChecked ? RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal;
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
stateRadio = bIsChecked ? RadioButtonState.CheckedDisabled : RadioButtonState.UncheckedDisabled;
// Determines bounds for text and radio button
sizeGlyph = RadioButtonRenderer.GetGlyphSize(e.Graphics, stateRadio);
ptGlyph = e.Bounds.Location;
ptGlyph.X += 4; // a comfortable distance from the edge
ptGlyph.Y += (e.Bounds.Height - sizeGlyph.Height) / 2;
// Draws the radio button
RadioButtonRenderer.DrawRadioButton(e.Graphics, ptGlyph, stateRadio);
}
else
{
CheckBoxState stateCheck = bIsChecked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
if ((e.State & DrawItemState.Disabled) == DrawItemState.Disabled || (e.State & DrawItemState.Grayed) == DrawItemState.Grayed)
stateCheck = bIsChecked ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
// Determines bounds for text and radio button
sizeGlyph = CheckBoxRenderer.GetGlyphSize(e.Graphics, stateCheck);
ptGlyph = e.Bounds.Location;
ptGlyph.X += 20; // a comfortable distance from the edge
ptGlyph.Y += (e.Bounds.Height - sizeGlyph.Height) / 2;
// Draws the radio button
CheckBoxRenderer.DrawCheckBox(e.Graphics, ptGlyph, stateCheck);
}
// Draws the text
Rectangle rectText = new Rectangle(ptGlyph.X + sizeGlyph.Width + 3, e.Bounds.Y, e.Bounds.Width - sizeGlyph.Width, e.Bounds.Height);
e.Graphics.DrawString(strText.Substring(4), e.Font, brushText, rectText, this.oAlign);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
protected override void OnItemCheck (ItemCheckEventArgs ice)
{
base.OnItemCheck(ice);
if (ice.NewValue == CheckState.Unchecked)
return;
if (this.GetItemType(ice.Index) == ItemType.Radio) // if they selected a root, deselect other roots and their children
{
for (Int32 i = 0; i < this.Items.Count; ++i)
{
if (i == ice.Index)
continue;
if (this.GetItemType(i) == ItemType.Radio)
{
this.SetItemChecked(i, false);
Int32 j = i + 1;
while (j < this.Items.Count && this.GetItemType(j) == ItemType.Checkbox)
{
this.SetItemChecked(j, false);
j++;
}
}
}
}
else if (this.GetItemType(ice.Index) == ItemType.Checkbox) // they selected a child; select the root too and deselect other roots and their children
{
// Find parent
Int32 iParentIdx = ice.Index - 1;
while (iParentIdx >= 0 && this.GetItemType(iParentIdx) == ItemType.Checkbox)
iParentIdx--;
this.SetItemChecked(iParentIdx, true);
}
}
protected ItemType GetItemType (Int32 iIdx)
{
String strText = this.Items[iIdx].ToString();
if (strText.StartsWith("(o)"))
return (ItemType.Radio);
else if (strText.StartsWith("[x]"))
return (ItemType.Checkbox);
throw (new Exception("Invalid item type"));
}

Resources