-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from alessandro-bugatti/guess_the_card
New example about maouse movement
- Loading branch information
Showing
7 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <ctime> | ||
#include "../../vsgl2.h" | ||
|
||
using namespace std; | ||
using namespace vsgl2; | ||
using namespace vsgl2::general; | ||
using namespace vsgl2::io; | ||
using namespace vsgl2::video; | ||
using namespace vsgl2::utils; | ||
using namespace vsgl2::ttf_fonts; | ||
|
||
const int dim = 250; | ||
const int card_width = 200; | ||
const int card_height = 281; | ||
const int top_padding = 100; | ||
const int width = 768; | ||
const int height = 512; | ||
const int start = (width - (dim * 2 + card_width)) / 2; | ||
|
||
void show_card_backs() | ||
{ | ||
draw_image("images/back.png",start,top_padding,card_width,card_height); | ||
draw_image("images/back.png",start + dim,top_padding,card_width,card_height); | ||
draw_image("images/back.png",start + 2*dim,top_padding,card_width,card_height); | ||
} | ||
|
||
void show_first() | ||
{ | ||
draw_image("images/queen.png",start,top_padding,card_width,card_height); | ||
draw_image("images/jack.png",start + dim,top_padding,card_width,card_height); | ||
draw_image("images/jack.png",start + 2*dim,top_padding,card_width,card_height); | ||
} | ||
|
||
void show_second() | ||
{ | ||
draw_image("images/jack.png",start,top_padding,card_width,card_height); | ||
draw_image("images/queen.png",start + dim,top_padding,card_width,card_height); | ||
draw_image("images/jack.png",start + 2*dim,top_padding,card_width,card_height); | ||
} | ||
|
||
void show_third() | ||
{ | ||
draw_image("images/jack.png",start,top_padding,card_width,card_height); | ||
draw_image("images/jack.png",start + dim,top_padding,card_width,card_height); | ||
draw_image("images/queen.png",start + 2*dim,top_padding,card_width,card_height); | ||
} | ||
|
||
void show_cards(int n) | ||
{ | ||
switch(n) | ||
{ | ||
case 0: show_card_backs(); | ||
break; | ||
case 1: show_first(); | ||
break; | ||
case 2: show_second(); | ||
break; | ||
case 3: show_third(); | ||
break; | ||
} | ||
} | ||
|
||
int choosen_card(int x, int y) | ||
{ | ||
if (x > start && x < start + card_width && | ||
y > top_padding && y < top_padding + card_height) | ||
return 1; | ||
if (x > start + dim && x < start + card_width + dim && | ||
y > top_padding && y < top_padding + card_height) | ||
return 2; | ||
if (x > start + dim*2&& x < start + card_width + dim*2 && | ||
y > top_padding && y < top_padding + card_height) | ||
return 3; | ||
return 0; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
char coordinates[20]; | ||
//init the library | ||
init(); | ||
//create the window and show it | ||
set_window(width, height, "Vsgl2 Guess The Card"); | ||
srand(time(NULL)); | ||
int computer_choice = rand()%3 + 1; | ||
int state = 0; | ||
int your_choice; | ||
set_background_color(Color(212,250,180,0)); | ||
while(!done()) | ||
{ | ||
draw_text("arvo.ttf",50,"Where is the Queen?",30,0, | ||
Color(50,50,155,255)); | ||
show_cards(state); | ||
if (mouse_left_button_pressed()) | ||
{ | ||
your_choice = choosen_card(get_mouse_x(), get_mouse_y()); | ||
if (your_choice != 0) | ||
{ | ||
show_cards(computer_choice); | ||
if (your_choice == computer_choice) | ||
draw_text("arvo.ttf",30,"YOU WON! Press a button to try again...",30, | ||
top_padding + card_height + 20 , | ||
Color(50,175,50,255)); | ||
else | ||
draw_text("arvo.ttf",30,"YOU LOST! Press a button to try again...",30, | ||
top_padding + card_height + 20, | ||
Color(255,0,0,255)); | ||
update(); | ||
wait_for_button_pressed(); | ||
computer_choice = rand()%3 + 1; | ||
} | ||
} | ||
update(); | ||
} | ||
close(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Mouse movements | ||
|
||
The VSGL2 library contains some functions to read the mouse state: | ||
- ```get_mouse_x()``` and ```get_mouse_y()``` to read the mouse coordinates | ||
- ```mouse_left_button_pressed()``` and ```mouse_right_button_pressed()``` to read the button status. | ||
|
||
In this example the first two functions are used to read the mouse coordinates and display them on the screen. | ||
|
||
![Example](images/example.png) | ||
|
||
Two other functions related to TrueType fonts are also used: ```text_width``` and ```text_height```. Their signature is: | ||
```c | ||
text_width( | ||
string font, | ||
int dim, | ||
string text | ||
) | ||
``` | ||
where **font** is the name of the font used, **dim** is the dimension, and **text** is the text to be displayed: these functions are useful to obtain the real dimension in pixels of the text to be displayed, because it depends on the font, dimension and text to be displayed. In this example, this information is used to show the mouse coordinates above the mouse position rather than below, using the following code: | ||
```c | ||
draw_text("audiowide.ttf",20,coordinates, | ||
x - text_width("audiowide.ttf", 20 ,coordinates), | ||
y - text_height("audiowide.ttf", 20, coordinates), | ||
Color(0,0,0,255)); | ||
``` | ||
|
||
The *width* and the *height* of the text are subtracted from *x* and *y* to move the text above the mouse position. | ||
|
||
The string **coordinates** is created using **sprintf** function in this way: | ||
```c | ||
sprintf(coordinates,"(%3d, %3d)",x ,y ); | ||
``` | ||
where **x** and **y** values are refreshed at each cycle of the main loop: | ||
```c | ||
x = get_mouse_x(); | ||
y = get_mouse_y(); | ||
``` | ||
|
||
The *delay(2)* instruction is inserted only so that the CPU is not fully loaded without necessity. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||
<CodeBlocks_project_file> | ||
<FileVersion major="1" minor="6" /> | ||
<Project> | ||
<Option title="vsgl2_guess_the_card" /> | ||
<Option pch_mode="2" /> | ||
<Option compiler="gcc" /> | ||
<Build> | ||
<Target title="Win32 Debug"> | ||
<Option output="win32/bin/Debug/$(PROJECTNAME)" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="win32/obj/Debug/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Win32 Release"> | ||
<Option output="win32/bin/Release/$(PROJECTNAME)" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="win32/obj/Release/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
<Add option="-lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Linux Debug"> | ||
<Option output="linux/bin/Debug/$(PROJECTNAME)" prefix_auto="1" extension_auto="0" /> | ||
<Option object_output="linux/obj/Debug/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Linux Release"> | ||
<Option output="linux/bin/Release/$(PROJECTNAME)" prefix_auto="1" extension_auto="0" /> | ||
<Option object_output="linux/obj/Release/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
<Add option="-lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
</Build> | ||
<Compiler> | ||
<Add option="-Wall" /> | ||
<Add option="-std=c++11" /> | ||
</Compiler> | ||
<Unit filename="../../vsgl2.cpp" /> | ||
<Unit filename="../../vsgl2.h" /> | ||
<Unit filename="main.cpp" /> | ||
<Extensions> | ||
<code_completion /> | ||
<envvars /> | ||
<debugger /> | ||
<lib_finder disable_auto="1" /> | ||
</Extensions> | ||
</Project> | ||
</CodeBlocks_project_file> |