From 7aed073fa6b9c7bfd8b16bd5b798e8d058e623e5 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Fri, 5 Jul 2024 00:46:02 -0400 Subject: [PATCH] moving repo --- src/arguments.h | 2 +- src/arguments.h.orig | 12 ++++++++++++ src/constants.h | 3 +++ src/cursor.cc | 30 ++++++++++++++++++++++++++++++ src/cursor.h | 6 +++++- src/display.h | 4 ++-- src/display.h.orig | 6 +++--- src/game.h | 6 +++--- src/game.h.orig | 8 ++++++-- src/main.cc | 12 ++++++------ src/main.cc.orig | 37 ++++++++++++++++++++++++------------- 11 files changed, 95 insertions(+), 31 deletions(-) create mode 100644 src/arguments.h.orig create mode 100644 src/cursor.cc diff --git a/src/arguments.h b/src/arguments.h index 7b72410..0b7b393 100644 --- a/src/arguments.h +++ b/src/arguments.h @@ -7,6 +7,6 @@ #include "constants.h" -feature proc_args(int argc, char ** argv, cursor &curse, input &in, logger& log); +feature proc_args(int argc, char **argv, cursor &curse, input &in, logger &log); #endif diff --git a/src/arguments.h.orig b/src/arguments.h.orig new file mode 100644 index 0000000..7b72410 --- /dev/null +++ b/src/arguments.h.orig @@ -0,0 +1,12 @@ +#ifndef __ARGUMENTS_H__ +#define __ARGUMENTS_H__ +#include "log.h" +#include "cursor.h" +#include "display.h" +#include "input.h" + +#include "constants.h" + +feature proc_args(int argc, char ** argv, cursor &curse, input &in, logger& log); + +#endif diff --git a/src/constants.h b/src/constants.h index d649c07..e2a9fe8 100644 --- a/src/constants.h +++ b/src/constants.h @@ -30,6 +30,9 @@ enum game_command {game_command_terminate = 0, game_command_pass, game_command_panic }; +// TODO: added a few colors for cursor +enum curse_color {}; + enum stat_name {HP, ATK, DEF, hostile}; const int LAYER_CNT = 4; // TODO: update as you go diff --git a/src/cursor.cc b/src/cursor.cc new file mode 100644 index 0000000..f35d571 --- /dev/null +++ b/src/cursor.cc @@ -0,0 +1,30 @@ +#include "cursor.h" +#include "constants.h" + +cursor::cursor() { + initscr(); +} + +cursor::~cursor() { + endwin(); +} + +int cursor::getcmd() const { + return getch(); +} + +void cursor::show() const { + refresh(); +} + +void cursor::print_char(const position &pos, const char ch) const { + +} + +bool check_terminal_size() { + int max_x; + int max_y; + getmaxyx(stdscr, max_y, max_x); + + return max_x >= DISPLAY_WIDTH && max_y >= DISPLAY_HEIGHT; +} diff --git a/src/cursor.h b/src/cursor.h index 8be54ce..188d0ae 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -20,9 +20,13 @@ public: void show() const; - void print_char(const position &pos) const; + void print_char(const position &pos, const char ch) const; void print_str(const position &head, const std::string str) const; }; +// IMPORTANT: this will fail when terminal size changes +// checks if terminal size fits the minimum requirements +bool check_terminal_size(); + #endif diff --git a/src/display.h b/src/display.h index 2f5fce3..03d0723 100644 --- a/src/display.h +++ b/src/display.h @@ -16,10 +16,10 @@ class display final { private: std::vector contents; - cursor &curse; + cursor &curse; public: display(); - display(cursor&new_curse,int argc, char **argv); + display(cursor &new_curse, int argc, char **argv); void clear(); // use this instead of overloading for clarity diff --git a/src/display.h.orig b/src/display.h.orig index 17aa02b..2f5fce3 100644 --- a/src/display.h.orig +++ b/src/display.h.orig @@ -16,17 +16,17 @@ class display final { private: std::vector contents; - cursor &curs; + cursor &curse; public: display(); - display(cursor &s); + display(cursor&new_curse,int argc, char **argv); void clear(); // use this instead of overloading for clarity void print(std::ostream &out) const; void print_line(const std::string &line, const int linenum); - void render() const ; + void render() const ; // will override any character that was there void print_position(const position &pos, const char ch); diff --git a/src/game.h b/src/game.h index a227c34..8fe47cb 100644 --- a/src/game.h +++ b/src/game.h @@ -13,16 +13,16 @@ class game final { private: display &out; - cursor &curse; + cursor &curse; feature features; std::unique_ptr rng; std::unique_ptr player; std::vector levels; public: - game(cursor&new_curse,display &new_display, int argc, char **argv); + game(cursor &new_curse, display &new_display, int argc, char **argv); game_status run(); private: - int getcmd() const; + int getcmd() const; }; #endif diff --git a/src/game.h.orig b/src/game.h.orig index bf2618f..a227c34 100644 --- a/src/game.h.orig +++ b/src/game.h.orig @@ -4,6 +4,7 @@ #include #include "constants.h" #include "display.h" +#include "cursor.h" #include "rng.h" #include "characters.h" #include "map.h" @@ -11,14 +12,17 @@ class game final { private: - display &d; + display &out; + cursor &curse; feature features; std::unique_ptr rng; std::unique_ptr player; std::vector levels; public: - game(display &new_display, int argc, char **argv); + game(cursor&new_curse,display &new_display, int argc, char **argv); game_status run(); +private: + int getcmd() const; }; #endif diff --git a/src/main.cc b/src/main.cc index 365285d..d3299d5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -13,18 +13,18 @@ // Notes: // 1. game sends all output to display and gets all input from input // 2. display either renders to file (console) or curse -// 3. +// 3. int main(int argc, char **argv) { - logger log; - cursor curse; - input in(curse); - feature enabled_features = proc_args(argc,argv,curse,in,log); + logger log; + cursor curse; + input in(curse); + feature enabled_features = proc_args(argc, argv, curse, in, log); display out(curse, enabled_features); // binds display to the game game game_proc(in, out, argc, argv); - + while (game_proc.run() != game_status::terminated) out.render(); diff --git a/src/main.cc.orig b/src/main.cc.orig index bf48e3d..365285d 100644 --- a/src/main.cc.orig +++ b/src/main.cc.orig @@ -1,21 +1,32 @@ #include "game.h" #include "cursor.h" #include "display.h" +#include "input.h" +#include "arguments.h" // The way things are designed to work: -// 1. initializes a new ncurses wrapper c -// Note: This is done the first thing in main because ncurses -// are from procedural programming and may break otherwise (unlikely) -// 2. bind c to a new display c (that handles all output) -// 3. bind d to g for graphics +// 1. out initializes a new display (this is a virtual one) +// 2. out initializes output based on given args +// This is when curse is initialized (curse.init()) +// 3. game_proc binds output to out and curses (if needed) + +// Notes: +// 1. game sends all output to display and gets all input from input +// 2. display either renders to file (console) or curse +// 3. int main(int argc, char **argv) { - cursor c; - // binds a ncurses cursor to display - display d(c); - // binds display to the game - game g(d,argc, argv); - while(g.run()!=game_status::terminated) - d.render(); - return 0; + logger log; + cursor curse; + input in(curse); + feature enabled_features = proc_args(argc,argv,curse,in,log); + display out(curse, enabled_features); + + // binds display to the game + game game_proc(in, out, argc, argv); + + while (game_proc.run() != game_status::terminated) + out.render(); + + return 0; }