Tetris: mouse proof-of-concept
This commit is contained in:
parent
4e81ac6562
commit
5562ca4d82
@ -10,7 +10,7 @@ OUTPUTFLAG = -Fo
|
|||||||
else
|
else
|
||||||
UNAME_S := $(shell uname -s)
|
UNAME_S := $(shell uname -s)
|
||||||
CXX ?= g++
|
CXX ?= g++
|
||||||
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 #-g -fsanitize=address
|
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 -DDEBUG #-g -fsanitize=address
|
||||||
OBJEXT = o
|
OBJEXT = o
|
||||||
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
|
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
|
||||||
OUTPUTFLAG = -o
|
OUTPUTFLAG = -o
|
||||||
|
@ -20,6 +20,16 @@
|
|||||||
#define MENU_ITEM_ID 0x10000001
|
#define MENU_ITEM_ID 0x10000001
|
||||||
#define MENU_BACKGROUND_ID 0x10000002
|
#define MENU_BACKGROUND_ID 0x10000002
|
||||||
#define MENU_TEXT_ID 0x10000003
|
#define MENU_TEXT_ID 0x10000003
|
||||||
|
#define MENU_RESUME_ID 0x10000004
|
||||||
|
#define MENU_OPTIONS_ID 0x10000005
|
||||||
|
#define MENU_RESTART_ID 0x10000006
|
||||||
|
#define MENU_QUIT_ID 0x10000007
|
||||||
|
#define MENU_MOUSE_ID 0x10000008
|
||||||
|
|
||||||
|
#define OPTIONS_COLOR_ID 0x10000009
|
||||||
|
#define OPTIONS_SHADOW_ID 0x1000000A
|
||||||
|
#define OPTIONS_3D_ID 0x1000000B
|
||||||
|
#define OPTIONS_SAVE_ID 0x1000000C
|
||||||
|
|
||||||
#define LEFT_BORDER 0.3
|
#define LEFT_BORDER 0.3
|
||||||
#define RIGHT_BORDER 0.7
|
#define RIGHT_BORDER 0.7
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "functions.hpp"
|
#include "functions.hpp"
|
||||||
#include "global_vars.hpp"
|
#include "global_vars.hpp"
|
||||||
|
#include "../sdlpp/sdlpp_mouse.hpp"
|
||||||
|
|
||||||
|
#include <SDL2/SDL_events.h>
|
||||||
|
#include <SDL2/SDL_mouse.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
constexpr uint64_t MAIN_MENU_RESUME = 0;
|
constexpr uint64_t MAIN_MENU_RESUME = 0;
|
||||||
@ -182,6 +185,8 @@ void addMenuSceneItems( SDLPP::Scene &scene,
|
|||||||
resume->setColor( colors["menu_item_background"] );
|
resume->setColor( colors["menu_item_background"] );
|
||||||
resume->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
resume->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
resume->setId( MENU_ITEM_ID );
|
resume->setId( MENU_ITEM_ID );
|
||||||
|
resume->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESUME_ID ) );
|
||||||
g_menu_options.push_back( resume );
|
g_menu_options.push_back( resume );
|
||||||
scene.addObject( resume );
|
scene.addObject( resume );
|
||||||
auto options =
|
auto options =
|
||||||
@ -189,6 +194,8 @@ void addMenuSceneItems( SDLPP::Scene &scene,
|
|||||||
options->setText( "Options", g_font_config );
|
options->setText( "Options", g_font_config );
|
||||||
options->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
options->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
options->setId( MENU_ITEM_ID );
|
options->setId( MENU_ITEM_ID );
|
||||||
|
options->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_OPTIONS_ID ) );
|
||||||
g_menu_options.push_back( options );
|
g_menu_options.push_back( options );
|
||||||
scene.addObject( options );
|
scene.addObject( options );
|
||||||
auto restart =
|
auto restart =
|
||||||
@ -196,6 +203,8 @@ void addMenuSceneItems( SDLPP::Scene &scene,
|
|||||||
restart->setText( "Restart", g_font_config );
|
restart->setText( "Restart", g_font_config );
|
||||||
restart->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
restart->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
restart->setId( MENU_ITEM_ID );
|
restart->setId( MENU_ITEM_ID );
|
||||||
|
restart->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESTART_ID ) );
|
||||||
g_menu_options.push_back( restart );
|
g_menu_options.push_back( restart );
|
||||||
scene.addObject( restart );
|
scene.addObject( restart );
|
||||||
auto quit =
|
auto quit =
|
||||||
@ -203,8 +212,17 @@ void addMenuSceneItems( SDLPP::Scene &scene,
|
|||||||
quit->setText( "Quit Game", g_font_config );
|
quit->setText( "Quit Game", g_font_config );
|
||||||
quit->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
quit->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
quit->setId( MENU_ITEM_ID );
|
quit->setId( MENU_ITEM_ID );
|
||||||
|
quit->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_QUIT_ID ) );
|
||||||
g_menu_options.push_back( quit );
|
g_menu_options.push_back( quit );
|
||||||
scene.addObject( quit );
|
scene.addObject( quit );
|
||||||
|
auto mouse =
|
||||||
|
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, r );
|
||||||
|
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
|
mouse->setId( MENU_MOUSE_ID );
|
||||||
|
mouse->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
|
||||||
|
scene.addObject( mouse );
|
||||||
}
|
}
|
||||||
|
|
||||||
void addGameOverSceneItems( SDLPP::Scene &scene,
|
void addGameOverSceneItems( SDLPP::Scene &scene,
|
||||||
@ -256,6 +274,8 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
|||||||
color_scheme->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
color_scheme->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
color_scheme->setColor( colors["menu_item_background"] );
|
color_scheme->setColor( colors["menu_item_background"] );
|
||||||
color_scheme->setId( MENU_ITEM_ID );
|
color_scheme->setId( MENU_ITEM_ID );
|
||||||
|
color_scheme->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_COLOR_ID ) );
|
||||||
g_options_options.push_back( color_scheme );
|
g_options_options.push_back( color_scheme );
|
||||||
scene.addObject( color_scheme );
|
scene.addObject( color_scheme );
|
||||||
auto shadow =
|
auto shadow =
|
||||||
@ -263,6 +283,8 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
|||||||
shadow->setText( "Show shadow: YES", g_font_config );
|
shadow->setText( "Show shadow: YES", g_font_config );
|
||||||
shadow->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
shadow->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
shadow->setId( MENU_ITEM_ID );
|
shadow->setId( MENU_ITEM_ID );
|
||||||
|
shadow->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SHADOW_ID ) );
|
||||||
g_options_options.push_back( shadow );
|
g_options_options.push_back( shadow );
|
||||||
scene.addObject( shadow );
|
scene.addObject( shadow );
|
||||||
auto show3d =
|
auto show3d =
|
||||||
@ -270,6 +292,8 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
|||||||
show3d->setText( "Show block texture: NO", g_font_config );
|
show3d->setText( "Show block texture: NO", g_font_config );
|
||||||
show3d->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
show3d->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
show3d->setId( MENU_ITEM_ID );
|
show3d->setId( MENU_ITEM_ID );
|
||||||
|
show3d->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_3D_ID ) );
|
||||||
g_options_options.push_back( show3d );
|
g_options_options.push_back( show3d );
|
||||||
scene.addObject( show3d );
|
scene.addObject( show3d );
|
||||||
auto save =
|
auto save =
|
||||||
@ -277,8 +301,17 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
|||||||
save->setText( "SAVE", g_font_config );
|
save->setText( "SAVE", g_font_config );
|
||||||
save->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
save->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
save->setId( MENU_ITEM_ID );
|
save->setId( MENU_ITEM_ID );
|
||||||
|
save->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SAVE_ID ) );
|
||||||
g_options_options.push_back( save );
|
g_options_options.push_back( save );
|
||||||
scene.addObject( save );
|
scene.addObject( save );
|
||||||
|
auto mouse =
|
||||||
|
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, r );
|
||||||
|
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
|
mouse->setId( MENU_MOUSE_ID );
|
||||||
|
mouse->addCollision(
|
||||||
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
|
||||||
|
scene.addObject( mouse );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr< SDLPP::Scene >
|
std::shared_ptr< SDLPP::Scene >
|
||||||
@ -552,6 +585,56 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool g_mouse_down = false;
|
||||||
|
int selected_menu_down = -1;
|
||||||
|
int selected_options_down = -1;
|
||||||
|
|
||||||
|
void highlightMenuItem(uint64_t id) {
|
||||||
|
g_menu_options[g_menu_select]->unsetColor();
|
||||||
|
switch(id) {
|
||||||
|
case MENU_RESUME_ID:
|
||||||
|
g_menu_select = 0;
|
||||||
|
break;
|
||||||
|
case MENU_OPTIONS_ID:
|
||||||
|
g_menu_select = 1;
|
||||||
|
break;
|
||||||
|
case MENU_RESTART_ID:
|
||||||
|
g_menu_select = 2;
|
||||||
|
break;
|
||||||
|
case MENU_QUIT_ID:
|
||||||
|
g_menu_select = 3;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(g_mouse_down)
|
||||||
|
return;
|
||||||
|
g_menu_options[g_menu_select]->setColor(
|
||||||
|
colors["menu_item_background"] );
|
||||||
|
}
|
||||||
|
|
||||||
|
void highlightOptionsItem(uint64_t id) {
|
||||||
|
g_options_options[g_options_select]->unsetColor();
|
||||||
|
switch(id) {
|
||||||
|
case OPTIONS_COLOR_ID:
|
||||||
|
g_options_select = 0;
|
||||||
|
break;
|
||||||
|
case OPTIONS_SHADOW_ID:
|
||||||
|
g_options_select = 1;
|
||||||
|
break;
|
||||||
|
case OPTIONS_3D_ID:
|
||||||
|
g_options_select = 2;
|
||||||
|
break;
|
||||||
|
case OPTIONS_SAVE_ID:
|
||||||
|
g_options_select = 3;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(g_mouse_down)
|
||||||
|
return;
|
||||||
|
g_options_options[g_options_select]->setColor(
|
||||||
|
colors["menu_item_background"] );
|
||||||
|
}
|
||||||
|
|
||||||
void pollEventsMenu() {
|
void pollEventsMenu() {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while ( SDLPP::getSDLEvent( event ) ) {
|
while ( SDLPP::getSDLEvent( event ) ) {
|
||||||
@ -569,6 +652,30 @@ void pollEventsMenu() {
|
|||||||
g_update_scenes.push_back( x );
|
g_update_scenes.push_back( x );
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEMOTION: {
|
||||||
|
auto mouse = g_menu_scene->getObjects({MENU_MOUSE_ID})[0];
|
||||||
|
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(g_menu_scene->getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
|
||||||
|
auto collisions = g_menu_scene->getCollisions(*mouse);
|
||||||
|
if(collisions.size() == 2) {
|
||||||
|
for(auto &collision : collisions) {
|
||||||
|
if(collision.second->getId() == MENU_ITEM_ID) {
|
||||||
|
highlightMenuItem(collision.second->getCollisions()[0]->getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
g_mouse_down = false;
|
||||||
|
if(selected_menu_down == g_menu_select) {
|
||||||
|
handleKeyDownMenu(SDLK_RETURN);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
g_mouse_down = true;
|
||||||
|
selected_menu_down = g_menu_select;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -794,6 +901,31 @@ void pollEventsOptions() {
|
|||||||
g_update_scenes.push_back( x );
|
g_update_scenes.push_back( x );
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEMOTION: {
|
||||||
|
auto mouse = g_options_scene->getObjects({MENU_MOUSE_ID})[0];
|
||||||
|
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(g_options_scene->getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
|
||||||
|
auto collisions = g_options_scene->getCollisions(*mouse);
|
||||||
|
if(collisions.size() == 2) {
|
||||||
|
for(auto &collision : collisions) {
|
||||||
|
if(collision.second->getId() == MENU_ITEM_ID) {
|
||||||
|
highlightOptionsItem(collision.second->getCollisions()[0]->getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
g_mouse_down = false;
|
||||||
|
if(selected_options_down == g_options_select) {
|
||||||
|
handleKeyDownOptions(SDLK_RIGHT);
|
||||||
|
handleKeyDownOptions(SDLK_RETURN);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
g_mouse_down = true;
|
||||||
|
selected_options_down = g_options_select;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user