diff --git a/application.cpp b/application.cpp index 1b75e7121b067cdea5608a8c30064c8fe7fe895e..9b038fe9b1a6be8e3a9d7dd91a03949abeff7293 100644 --- a/application.cpp +++ b/application.cpp @@ -15,7 +15,7 @@ void Application::event_loop() int focus = -1, szam =1; bool in_game = false; // Új változó a játék állapotának nyomon követésére - while(gin >> ev) + while(gin >> ev && ev.keycode != key_escape) { if (!in_game) // Amíg nincs a játékban, ellenőrizzük a "Play" gombot { @@ -95,7 +95,7 @@ void Application::event_loop() { if ((puppets[i]->getColor() && puppets[i]->getY() == 560) || (!puppets[i]->getColor() && puppets[i]->getY() == 0)) { - if(puppets[focus]->getX() == puppets[i]->getX() and puppets[focus]->getY() == puppets[i]->getY() ) + if(puppets[focus]->getX() == puppets[i]->getX() and puppets[focus]->getY() == puppets[i]->getY() and puppets[i]->is_pawn()) { removePuppet(puppets[i]); background(); diff --git a/graphics.hpp b/graphics.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e0218117db40afca8b51161a6d826337beb3dced --- /dev/null +++ b/graphics.hpp @@ -0,0 +1,296 @@ +#ifndef GRAPHICS_HPP_INCLUDED +#define GRAPHICS_HPP_INCLUDED + +#include <string> +#include <vector> + +struct SDL_Surface; +struct SDL_Window; +struct _TTF_Font; +struct SDL_Renderer; + +namespace genv +{ + +/*********** Graphical output device definition ***********/ + +class canvas { +public: + canvas(); + virtual ~canvas(); + canvas(int w, int h); + canvas(const canvas & c); + genv::canvas& operator=(const genv::canvas &c); + bool open(unsigned width, unsigned height); + bool save(const std::string& file) const; + void transparent(bool t) {transp=t;} + void set_color(int r, int g, int b); + bool move_point(int x, int y); + void draw_dot(); + void draw_line(int x, int y); + void draw_box(int x, int y); + void draw_text(const std::string& str); + void blitfrom(const canvas &c, short x1, short y1, int x2, int y2, short x3, short y3); + void line(int x1, int y1, int x2, int y2, unsigned char r, unsigned char g, unsigned char b); + void dot(int x, int y, unsigned char r, unsigned char g, unsigned char b) { + memorybuf[y*memorypitch + x]=(b+256*g+65536*r); + } + + bool load_font(const std::string& fname, int fontsize = 16, bool antialias=true); + void set_antialias(bool antialias) {antialiastext=antialias;} + + int x() const { return pt_x; } + int y() const { return pt_y; } + + int cascent() const; + int cdescent() const; + + int twidth(const std::string& s) const; + + virtual void refresh() {} + + template <typename T> + inline void call_with_rel(T meth, int vec_x, int vec_y) { + if (vec_x || vec_y) { + int dx=vec_x-sgn(vec_x); + int dy=vec_y-sgn(vec_y); + (this->*meth)(dx, dy); + } + } + +protected: + + template <typename T> + inline int sgn(const T& a) { + if (a<0) return -1; + if (a>0) return 1; + return 0; + } + + short pt_x; + short pt_y; + SDL_Surface* buf; + int draw_clr; + bool transp; + _TTF_Font* font; + bool antialiastext; + std::string loaded_font_file_name; + int font_size; + + int * memorybuf=0; + int memorypitch=0; + +}; + + + + +// Class of output device (singleton) +class groutput : public canvas +{ +public: + static groutput& instance(); + virtual ~groutput(); + + void showmouse(bool toggle); + void movemouse(int x, int y); + bool open(unsigned width, unsigned height, bool fullscreen=false); + virtual void refresh(); + void set_title(const std::string& title); + void message(std::string errortext); + +private: + SDL_Window * window; + SDL_Renderer* renderer; + groutput(); +}; + +// Global accessor for the output device instance +extern groutput& gout; + +// Generic operator for applying global manipulators +template <typename Op> +inline canvas& operator << (canvas& out, Op oper) +{ oper(out); return out; } + + +// Global manipulators for frequently used operations +inline void refresh(canvas& out) { out.refresh(); } +inline void dot(canvas& out) { out.draw_dot(); } + +struct stamp +{ + canvas &c; + int x1,y1,x2,y2,x3,y3; + stamp(canvas&cc, int sx1, int sy1, int xsize, int ysize, int tx1, int ty1) : + c(cc), x1(sx1), y1(sy1),x2(xsize), y2(ysize), x3(tx1), y3(ty1) {} + stamp(canvas&cc, int tx1, int ty1) : + c(cc), x1(-1), y1(-1),x2(-1), y2(-1), x3(tx1), y3(ty1) {} + void operator () (canvas& out) + { out.blitfrom(c,x1,y1,x2,y2,x3,y3); } +}; + +struct color +{ + int red, green, blue; + color(int r, int g, int b) : red(r), green(g), blue(b) {} + void operator () (canvas& out) + { out.set_color(red, green, blue); } +}; + + + +struct move +{ + int vec_x, vec_y; + move(int x, int y) : vec_x(x), vec_y(y) {} + void operator () (canvas& out) + { out.move_point(vec_x, vec_y); } +}; + +struct move_to +{ + int pos_x, pos_y; + move_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {} + void operator () (canvas& out) + { out.move_point(pos_x - out.x(), pos_y - out.y()); } +}; + +struct line +{ + int vec_x, vec_y; + line(int x, int y) : vec_x(x), vec_y(y) {} + void operator () (canvas& out) + { out.call_with_rel(&canvas::draw_line,vec_x, vec_y); } +}; + + +struct line_to +{ + int pos_x, pos_y; + line_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {} + void operator () (canvas& out) + { out.draw_line(pos_x - out.x(), pos_y - out.y()); } +}; + +struct box +{ + int vec_x, vec_y; + box(int x, int y) : vec_x(x), vec_y(y) {} + void operator () (canvas& out) + { out.call_with_rel(&canvas::draw_box, vec_x, vec_y); } +}; + +struct box_to +{ + int pos_x, pos_y; + box_to(unsigned x, unsigned y) : pos_x(x), pos_y(y) {} + void operator () (canvas& out) + { out.draw_box(pos_x - out.x(), pos_y - out.y()); } +}; + +struct text +{ + std::string str; + text(const std::string& s) : str(s) {} + text(char c) : str(1, c) {} + void operator () (canvas& out) + { out.draw_text(str); } +}; +/* +struct title +{ + std::string str; + title(const std::string& s) : str(s) {} + void operator () (canvas& out) + { out.set_title(str); } +}; +*/ + +struct font +{ + std::string font_name; + int font_size; + bool antialias; + font(const std::string& s, int fs, bool a=true) : font_name(s), font_size(fs), antialias(a) {} + void operator () (canvas& out) + { out.load_font(font_name, font_size, antialias); } +}; + + +/*********** Input device definition **********/ + +// Keycode constants +enum keycode_t { + key_tab = '\t', key_backspace = '\b', key_enter = '\r', + key_escape = '\033', key_space = ' ', + key_up = 0x10000, key_down, key_right, key_left, + key_insert, key_delete, key_home, key_end, key_pgup, key_pgdn, + key_lshift, key_rshift, key_lctrl, key_rctrl, key_lalt, key_ralt, + key_lwin, key_rwin, key_menu, key_numl, key_capsl, key_scrl, + key_f0 = 0x20000, key_f1, key_f2, key_f3, key_f4, key_f5, key_f6, key_f7, + key_f8, key_f9, key_f10, key_f11, key_f12, key_f13, key_f14, key_f15 +}; + +enum button_t { + btn_left = 1, btn_middle, btn_right, btn_wheelup, btn_wheeldown +}; + +enum event_type { + ev_key = 1, ev_mouse, ev_timer +}; + +// Event descriptor +struct event +{ + int keycode; + int pos_x, pos_y; + int button; + int time; + int type; + std::string keyname; + std::string keyutf8; + +}; + +// Class of input device (singleton) +class grinput +{ +public: + static grinput& instance(); + + grinput& wait_event(event&); + void timer(int wait); + + operator const void*() const + { + if (quit) return 0; + else + return this; } +private: + void textmode(bool on); + grinput() : quit(false) {textmode(true);} + bool quit; +}; + +// Global accessor for the input device instance +extern grinput& gin; + +// Event reader operator +inline grinput& operator >> (grinput& inp, event& ev) +{ return inp.wait_event(ev); } + +std::vector<int> utf8_character_index(std::string str); + +std::vector<std::string> utf8_character_split(std::string str) ; + +inline std::string utf8_remove_last(std::string str) { + if (str=="") + return ""; + std::vector<int> v = utf8_character_index(str); + return str.substr(0,v.at(v.size()-2)); +} + +} + +#endif // GRAPHICS_HPP_INCLUDED diff --git a/king.cpp b/king.cpp index 4d9fe8203cb390abfd753e0fe8e53fee3f79c7fa..6be05f25bcc160e0f8726f54939d593c2e0bdfdc 100644 --- a/king.cpp +++ b/king.cpp @@ -129,7 +129,7 @@ bool King :: allowed1(int x, int y) void King :: Pos_steps1( int c, int d) { - /* + _Pos_steps1.clear(); _x1 = c, _y1 = d; //1 @@ -243,7 +243,7 @@ void King :: Pos_steps1( int c, int d) _Pos_steps1.push_back(make_pair(a,b)); } } - */ + } diff --git a/main.cpp b/main.cpp index 7515cc4a82a829ab3fd61527b9be4bde2004279a..3a6bb76fcb9482f89f001d70fa49226dc1a54ac5 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,10 @@ public: } void init_pieces() { - // Fehér bábuk inicializálása + r1_1 = new Rook(this, 0, 0, true); + r1_2 = new Rook(this, 560, 0, true); + r2_1 = new Rook(this, 0, 560, false); + k2 = new King(this, 240, 560, false); r1_1 = new Rook(this, 0, 0, true); kn1_1 = new Knight(this, 80, 0, true); b1_1 = new Bishop(this, 160, 0, true); @@ -57,7 +60,6 @@ public: p2_6 = new Pawn(this, 400, 480, false); p2_7 = new Pawn(this, 480, 480, false); p2_8 = new Pawn(this, 560, 480, false); - } void action(string command) override { diff --git a/pawn.cpp b/pawn.cpp index 019c180a5b534754613100780f7264138e5bd59e..60e0d39f01ae70f2ba506d5efec8786b0dda6c09 100644 --- a/pawn.cpp +++ b/pawn.cpp @@ -131,6 +131,10 @@ } } + bool Pawn :: is_pawn() + { + return true; + } void Pawn :: Pos_steps1(int c, int d) { diff --git a/pawn.hpp b/pawn.hpp index 0f3986ebe55d8cc9ee8cfd0f7bf61fc245140568..a1c5a1199b6d7d8b00d2609df1ea2448cf1d9e6d 100644 --- a/pawn.hpp +++ b/pawn.hpp @@ -13,6 +13,7 @@ public: Pawn(Application*, int x, int y, bool color); void draw() override; void handle(event) override; + bool is_pawn() override; void Pos_steps() override; void Pos_steps1(int c, int d) override; diff --git a/puppets.cpp b/puppets.cpp index e7f476a02efeecaf9bedfc3c3fec923290a5ac9f..3a3f1132319635734b772baedae0f3a227d38385 100644 --- a/puppets.cpp +++ b/puppets.cpp @@ -178,3 +178,7 @@ bool Puppets::is_king() { return false; } +bool Puppets::is_pawn() +{ + return false; +} diff --git a/puppets.hpp b/puppets.hpp index 93b84d6ab4470c0293355259e868a734729e1687..940f263f405fd849cc95d521226cab948fd7184c 100644 --- a/puppets.hpp +++ b/puppets.hpp @@ -20,6 +20,7 @@ public: virtual void Pos_steps() = 0; virtual void Pos_steps1(int c, int d) = 0; virtual bool is_king(); + virtual bool is_pawn(); //virtual void sakk(); virtual void sakkmatt(); bool inside(int x,int y);