- Treballant en algo de UI
This commit is contained in:
@@ -14,6 +14,8 @@ namespace draw
|
|||||||
SDL_Renderer *sdl_renderer = nullptr; // El renderer de SDL
|
SDL_Renderer *sdl_renderer = nullptr; // El renderer de SDL
|
||||||
SDL_Texture *sdl_texture = nullptr; // La textura de SDL a la que pintarem la nostra superficie "screen" i que despres volcarem a pantalla
|
SDL_Texture *sdl_texture = nullptr; // La textura de SDL a la que pintarem la nostra superficie "screen" i que despres volcarem a pantalla
|
||||||
|
|
||||||
|
static int screen_zoom = 1;
|
||||||
|
|
||||||
surface *screen = nullptr; // La superficie screen, que representa la pantalla. Se crea i destrueix internament
|
surface *screen = nullptr; // La superficie screen, que representa la pantalla. Se crea i destrueix internament
|
||||||
surface *destination = nullptr; // Punter a la actual superficie de destí
|
surface *destination = nullptr; // Punter a la actual superficie de destí
|
||||||
surface *source = nullptr; // Punter a la actual superficie d'oritge
|
surface *source = nullptr; // Punter a la actual superficie d'oritge
|
||||||
@@ -29,6 +31,8 @@ namespace draw
|
|||||||
// Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla
|
// Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla
|
||||||
void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom)
|
void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom)
|
||||||
{
|
{
|
||||||
|
screen_zoom = zoom;
|
||||||
|
|
||||||
// [TODO] Incloure gestió de pantalla completa
|
// [TODO] Incloure gestió de pantalla completa
|
||||||
|
|
||||||
// Inicialització de les estructures de SDL
|
// Inicialització de les estructures de SDL
|
||||||
@@ -72,6 +76,11 @@ namespace draw
|
|||||||
screen = destination = source = nullptr;
|
screen = destination = source = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const int getZoom()
|
||||||
|
{
|
||||||
|
return screen_zoom;
|
||||||
|
}
|
||||||
|
|
||||||
// Crea una superficie i torna un punter a ella
|
// Crea una superficie i torna un punter a ella
|
||||||
surface *createSurface(const uint16_t w, const uint16_t h)
|
surface *createSurface(const uint16_t w, const uint16_t h)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ namespace draw
|
|||||||
/// @brief Finalització del sistema (tancar coses de SDL, superficies fixes, etc...)
|
/// @brief Finalització del sistema (tancar coses de SDL, superficies fixes, etc...)
|
||||||
void quit();
|
void quit();
|
||||||
|
|
||||||
|
const int getZoom();
|
||||||
|
|
||||||
/// @brief Crea una superficie i torna un punter a ella
|
/// @brief Crea una superficie i torna un punter a ella
|
||||||
/// @param w ample de la superficie
|
/// @param w ample de la superficie
|
||||||
/// @param h alt de la superficie
|
/// @param h alt de la superficie
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace game
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
game::init();
|
game::init();
|
||||||
input::init();
|
input::init(draw::getZoom());
|
||||||
|
|
||||||
static unsigned int current_ticks = SDL_GetTicks();
|
static unsigned int current_ticks = SDL_GetTicks();
|
||||||
|
|
||||||
@@ -35,6 +35,10 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
input::updateKeypressed(e.key.keysym.scancode);
|
input::updateKeypressed(e.key.keysym.scancode);
|
||||||
}
|
}
|
||||||
|
if (e.type==SDL_MOUSEBUTTONUP)
|
||||||
|
{
|
||||||
|
input::updateClk(e.button.button);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
|
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
|
||||||
@@ -42,6 +46,7 @@ int main(int argc, char *argv[])
|
|||||||
if (!game::loop()) should_exit = true;
|
if (!game::loop()) should_exit = true;
|
||||||
input::updateKey(SDL_SCANCODE_UNKNOWN);
|
input::updateKey(SDL_SCANCODE_UNKNOWN);
|
||||||
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
|
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
|
||||||
|
input::updateClk(0);
|
||||||
current_ticks = SDL_GetTicks();
|
current_ticks = SDL_GetTicks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
#include "jinput.h"
|
#include "jinput.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include "jdraw.h"
|
||||||
|
|
||||||
namespace input
|
namespace input
|
||||||
{
|
{
|
||||||
static const uint8_t *keys = nullptr;
|
static const uint8_t *keys = nullptr;
|
||||||
static uint8_t keypressed = 0;
|
static uint8_t keypressed = 0;
|
||||||
static uint8_t keydown = 0;
|
static uint8_t keydown = 0;
|
||||||
|
static uint8_t btnClicked = 0;
|
||||||
|
static int screen_zoom = 1;
|
||||||
|
|
||||||
void init()
|
void init(const int zoom)
|
||||||
{
|
{
|
||||||
keys = SDL_GetKeyboardState(NULL);
|
keys = SDL_GetKeyboardState(NULL);
|
||||||
|
screen_zoom = zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determina si la tecla especificada està sent polsada ara mateix
|
// Determina si la tecla especificada està sent polsada ara mateix
|
||||||
@@ -46,22 +50,33 @@ namespace input
|
|||||||
keypressed = key;
|
keypressed = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateClk(uint8_t btn)
|
||||||
|
{
|
||||||
|
btnClicked = btn;
|
||||||
|
}
|
||||||
|
|
||||||
int mouseX()
|
int mouseX()
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
SDL_GetMouseState(&x, NULL);
|
SDL_GetMouseState(&x, NULL);
|
||||||
return x;
|
return x/screen_zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mouseY()
|
int mouseY()
|
||||||
{
|
{
|
||||||
int y;
|
int y;
|
||||||
SDL_GetMouseState(NULL, &y);
|
SDL_GetMouseState(NULL, &y);
|
||||||
return y;
|
return y/screen_zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mouseBtn(int btn)
|
bool mouseBtn(int btn)
|
||||||
{
|
{
|
||||||
return (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(btn));
|
return (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(btn));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mouseClk(int btn)
|
||||||
|
{
|
||||||
|
return btnClicked == btn;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
namespace input
|
namespace input
|
||||||
{
|
{
|
||||||
/// @brief Inicialitza els sistemes de teclat, ratolí i gamepad
|
/// @brief Inicialitza els sistemes de teclat, ratolí i gamepad
|
||||||
void init();
|
void init(const int zoom);
|
||||||
|
|
||||||
/// @brief Determina si la tecla especificada està sent polsada ara mateix
|
/// @brief Determina si la tecla especificada està sent polsada ara mateix
|
||||||
/// @param key tecla a consultar
|
/// @param key tecla a consultar
|
||||||
@@ -27,9 +27,11 @@ namespace input
|
|||||||
|
|
||||||
void updateKey(uint8_t key);
|
void updateKey(uint8_t key);
|
||||||
void updateKeypressed(uint8_t key);
|
void updateKeypressed(uint8_t key);
|
||||||
|
void updateClk(uint8_t btn);
|
||||||
|
|
||||||
int mouseX();
|
int mouseX();
|
||||||
int mouseY();
|
int mouseY();
|
||||||
bool mouseBtn(int btn);
|
bool mouseBtn(int btn);
|
||||||
|
bool mouseClk(int btn);
|
||||||
|
|
||||||
}
|
}
|
||||||
23
source/jui.cpp
Normal file
23
source/jui.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include "jui.h"
|
||||||
|
#include "jdraw.h"
|
||||||
|
#include "jinput.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
namespace ui
|
||||||
|
{
|
||||||
|
const bool button(const char *label, const int x, const int y, const int w, const int h)
|
||||||
|
{
|
||||||
|
const int mx = input::mouseX();
|
||||||
|
const int my = input::mouseY();
|
||||||
|
const bool btnDown = input::mouseBtn(1);
|
||||||
|
const bool inside = (mx>=x) && (mx<x+w) && (my>=y) && (my<y+h);
|
||||||
|
const int txt_size = strlen(label)*4;
|
||||||
|
const int txt_x = x+(w-txt_size)/2;
|
||||||
|
|
||||||
|
draw::color(inside?(btnDown?15:13):5);
|
||||||
|
draw::fillrect(x, y, w, h);
|
||||||
|
draw::print(label, 1+txt_x, y+3, 15, 8);
|
||||||
|
|
||||||
|
return inside && input::mouseClk(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
source/jui.h
Normal file
6
source/jui.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ui
|
||||||
|
{
|
||||||
|
const bool button(const char *label, const int x, const int y, const int w, const int h);
|
||||||
|
}
|
||||||
@@ -4,15 +4,16 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "actor.h"
|
#include "actor.h"
|
||||||
#include "room.h"
|
#include "room.h"
|
||||||
|
#include "jui.h"
|
||||||
|
|
||||||
draw::surface *surf;
|
draw::surface *surf;
|
||||||
actor::actor_t *box;
|
actor::actor_t *box;
|
||||||
int ii = 0;
|
int room_w = 0;
|
||||||
|
|
||||||
void restart()
|
void restart()
|
||||||
{
|
{
|
||||||
actor::clear();
|
actor::clear();
|
||||||
room::load(ii,3);
|
room::load(room_w,3);
|
||||||
|
|
||||||
box = actor::create("ASCENSOR",{16,32,0}, {8,8,4}, {64,0,32,24}, {0,24});
|
box = actor::create("ASCENSOR",{16,32,0}, {8,8,4}, {64,0,32,24}, {0,24});
|
||||||
box->flags = FLAG_MOVING;
|
box->flags = FLAG_MOVING;
|
||||||
@@ -67,12 +68,6 @@ int sx=1, sy=0;
|
|||||||
|
|
||||||
bool game::loop()
|
bool game::loop()
|
||||||
{
|
{
|
||||||
if ( input::keyPressed(SDL_SCANCODE_Q) )
|
|
||||||
{
|
|
||||||
ii++;if(ii>3)ii=0;
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
actor::update(actor::getFirst());
|
actor::update(actor::getFirst());
|
||||||
actor::reorder();
|
actor::reorder();
|
||||||
|
|
||||||
@@ -82,12 +77,20 @@ bool game::loop()
|
|||||||
room::draw2();
|
room::draw2();
|
||||||
//draw::draw(148+sx*2-sy*2, 67+sx+sy,24,24,24,0);
|
//draw::draw(148+sx*2-sy*2, 67+sx+sy,24,24,24,0);
|
||||||
|
|
||||||
print(0,0,input::mouseX()/3);
|
print(0,0,input::mouseX());
|
||||||
print(0,20,input::mouseY()/3);
|
print(0,20,input::mouseY());
|
||||||
print(0,30,input::mouseBtn(1)?1:0);
|
print(0,30,input::mouseBtn(1)?1:0);
|
||||||
print(0,40,input::mouseBtn(2)?1:0);
|
print(0,40,input::mouseBtn(2)?1:0);
|
||||||
print(0,50,input::mouseBtn(3)?1:0);
|
print(0,50,input::mouseBtn(3)?1:0);
|
||||||
draw::print("HOLA!", 30, 10, 14, 8);
|
|
||||||
|
char buffer[100];
|
||||||
|
draw::print("ROOM WIDTH:", 330, 13, 15, 0);
|
||||||
|
if (ui::button(SDL_itoa(room_w, buffer, 10), 380, 10, 28, 11))
|
||||||
|
{
|
||||||
|
room_w++;if(room_w>3)room_w=0;
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
||||||
draw::render();
|
draw::render();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user