/[anoncvs]/projects/roguelike/core.cpp


UCC Code Repository

Contents of /projects/roguelike/core.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations) (download)
Sat Apr 15 15:02:52 2006 UTC (15 years ago) by banana
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +4 -2 lines
Windows frontend finished (though it is rudimentary)

1 #include "core.h"
2 #include "port.h"
3
4 #include "DataFile.h"
5 #include "Terrain.h"
6 #include "View.h"
7 #include "World.h"
8 #include "Player.h"
9 #include "NPC.h"
10 using namespace std;
11
12 //Globals
13 raw_screen main_term; //buffer to draw into
14 World *main_world;
15 View *main_view;
16
17 Player *player;
18
19 // Hooks for ports
20 void core_init(int x, int y) {
21 DataFile *loader;
22
23 //Load data
24 for (int i = 0; i <= kMaxTerrainID; i++) {
25 loader = new DataFile("terrain", i);
26 Terrain::cache[i] = loader->asTerrain();
27 delete(loader);
28 }
29
30 //Create world
31 loader = new DataFile("world", 0);
32 main_world = loader->asWorld();
33 delete(loader);
34
35 //Create initial view
36 main_view = new View(main_world);
37 main_view->resize(x, y);
38 // main_view->move(0, 2);
39
40 //Initialise @ and demo mob
41 player = new Player();
42 main_world->add(player, 1, 1);
43 NPC *monster = new NPC(L"Dragon", L'D', 0xFF0000, main_world);
44 main_world->add(monster, 16, 3);
45 monster = new NPC(L"Rat", L'r', 0xbbbbbb, main_world);
46 main_world->add(monster, 16, 5);
47
48 main_term = main_view->render();
49 port_display(main_term); //XXX This should return the view, so that the frontend can control it
50 }
51
52 void core_event(wchar_t key) {
53 //XXX Hacked up demo 'control scheme' - move the @ around with the numpad, or quit with q
54 int new_x, new_y;
55 new_x = main_world->px;
56 new_y = main_world->py;
57
58 if (key == L'b') {
59 new_y++;
60 new_x--;
61 } else if (key == L'j') {
62 new_y++;
63 } else if (key == L'n') {
64 new_y++;
65 new_x++;
66 } else if (key == L'h') {
67 new_x--;
68 } else if (key == L'l') {
69 new_x++;
70 } else if (key == L'y') {
71 new_x--;
72 new_y--;
73 } else if (key == L'k') {
74 new_y--;
75 } else if (key == L'u') {
76 new_x++;
77 new_y--;
78 } else if (key == L'q') {
79 core_quit();
80 } else if (key == L' ') {
81 player->time += player->moveDelay();
82 } else {
83 }
84
85 if (new_x != main_world->px || new_y != main_world->py) {
86 main_world->move(player, new_x, new_y);
87 }
88 //XXX This is redundant, but for now we need to keep track of the player's x and y
89 if (!main_world->at(main_world->px, main_world->py).character) {
90 main_world->px = new_x;
91 main_world->py = new_y;
92 }
93
94 //Having finished the player's action, give everything else a go - if it's still our turn
95 //we'll come back in via the next keyboard event
96 main_world->act();
97
98 //XXX This should return void, the frontend can update itself
99 //Or should it.. hm. View != Screen, as there are stats and messages and stuff
100
101 core_display();
102 }
103
104 void core_quit() {
105 port_quit();
106 }
107
108 void core_display() {
109 free(main_term);
110 main_term = main_view->render();
111 port_display(main_term);
112 }
113
114
115 // For debugging in GDB
116 char *wcstostr(const wchar_t *wcs) {
117 char *buf;
118
119 buf = (char*)malloc(wcslen(wcs) + 1);
120
121 for (unsigned int i = 0; i < wcslen(wcs); i++) {
122 buf[i] = wctob(wcs[i]);
123 }
124
125 buf[wcslen(wcs)] = '\0';
126
127 return buf;
128 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26