|
From: <sch...@us...> - 2008-11-22 12:30:50
|
Revision: 49
http://deraciel.svn.sourceforge.net/deraciel/?rev=49&view=rev
Author: schnippi001
Date: 2008-11-22 12:30:44 +0000 (Sat, 22 Nov 2008)
Log Message:
-----------
Separated stats enumeration from player file
Added stats and map windows
Removed multiline output
Modified Paths:
--------------
trunk/INSTALL
trunk/char.h
trunk/keyboard.c
trunk/keyboard.h
trunk/main.c
trunk/text_window.c
trunk/text_window.h
Added Paths:
-----------
trunk/map_window.c
trunk/map_window.h
trunk/stat.h
trunk/stat_window.c
trunk/stat_window.h
Modified: trunk/INSTALL
===================================================================
--- trunk/INSTALL 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/INSTALL 2008-11-22 12:30:44 UTC (rev 49)
@@ -1,7 +1,7 @@
Compile
=======
-clear && gcc -std=c99 -o main *.c -lncurses -lform -lpthread
+clear && gcc -g -std=c99 -Wall -o main *.c -lncurses -lpthread
Run
Modified: trunk/char.h
===================================================================
--- trunk/char.h 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/char.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -4,6 +4,7 @@
#include <stdint.h>
#include "inventory.h"
+#include "stat.h"
enum
{
@@ -20,7 +21,7 @@
{
char name[32];
- uint8_t stats[STAT_MAX_STATS];
+ stat_t stats[STAT_MAX_STATS];
uint32_t experience;
uint8_t level;
@@ -28,5 +29,5 @@
struct invt_struct inventory;
};
-#endif
+#endif // !DERACIEL_CHAR_H
Modified: trunk/keyboard.c
===================================================================
--- trunk/keyboard.c 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/keyboard.c 2008-11-22 12:30:44 UTC (rev 49)
@@ -6,45 +6,58 @@
#include <pthread.h>
-pthread_t keyreader_thread;
+static pthread_t keyreader_thread;
static void (*handler_cb)(const int key);
-
-
+/*
+ The keyreader is an infinite loop that gets one character at iteration and
+ passes it to a keyboard handling callback. The callback then can respond
+ appropriately to the given key. This function is running inside a thread
+ created by keyreader_create().
+*/
static void *keyreader(void *unused)
{
- int tid;
- int ch;
+ /* This variable will hold the keycode of pressed key */
+ int key;
+ /* Run an infinite loop */
while(1) {
- ch = getch();
- if (handler_cb) handler_cb(ch);
+ /* Fetch a key from keyboard buffer */
+ key = getch();
+
+ /* If handler callback is specified, delegate the previously
+ fetched key to it, else throw an error message */
+ if (handler_cb) {
+ handler_cb(key);
+ } else {
+ fprintf(stderr, "ERROR: Handler callback was NULL!\n");
+ }
}
+
+ return NULL;
}
-
+/*
+ Creates a thread that runs keyreader().
+*/
void keyreader_create()
{
int rc = pthread_create(&keyreader_thread, NULL, keyreader, NULL);
- if (rc){
- printf("ERROR; return code from pthread_create() is %d\n", rc);
+ if (rc) {
+ fprintf(stderr, "ERROR: Keyreader thread creation failed! (error code: %d)\n", rc);
exit(-1);
}
}
-void keyreader_destroy()
-{
-}
-
-
-
void keyreader_set_handler(void(*cb)(const int key))
{
- // TODO: Error handling
+ /* If the callback is not NULL set it as active keyboard handling function */
if (cb) {
handler_cb = cb;
+ } else {
+ fprintf(stderr, "ERROR: Specified callback was NULL!\n");
}
}
Modified: trunk/keyboard.h
===================================================================
--- trunk/keyboard.h 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/keyboard.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -5,8 +5,6 @@
void keyreader_create(void);
-void keyreader_destroy(void);
-
void keyreader_set_handler(void(*cb)(const int key));
#endif // !DERACIEL_KEYBOARD_H
Modified: trunk/main.c
===================================================================
--- trunk/main.c 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/main.c 2008-11-22 12:30:44 UTC (rev 49)
@@ -2,21 +2,18 @@
#include <stdio.h>
#include <signal.h>
#include <time.h>
+#include <ncurses.h>
-#include <curses.h>
-
#include "text_window.h"
+#include "stat_window.h"
+#include "map_window.h"
#include "random.h"
#include "keyboard.h"
-void finish(const int);
-void repaint_windows(void);
-WINDOW *MAPWND;
-WINDOW *STATWND;
-
#define KEY_ESCAPE 27
+static void finish(const int);
static void test_keyhandler(const int key);
int main(int argc, char **argv)
@@ -33,11 +30,15 @@
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_YELLOW);
+ init_pair(3, COLOR_WHITE, COLOR_RED);
+ init_pair(4, COLOR_WHITE, COLOR_GREEN);
clear();
refresh();
text_window_create();
+ map_window_create();
+ stat_window_create();
/* Create keyboard handler */
keyreader_create();
@@ -48,7 +49,6 @@
/* Let thread work */
while(1) { sleep(5); };
- text_window_destroy();
finish(0);
return 0;
}
@@ -59,6 +59,8 @@
{
/* If text window is still existing, destroy it */
text_window_destroy();
+ map_window_destroy();
+ stat_window_destroy();
/* End ncurses library */
endwin();
@@ -69,9 +71,13 @@
}
static int counter = 0;
+static chtype map[80*20 + 1];
+static stat_t stats[STAT_MAX_STATS] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
static void test_keyhandler(const int key)
{
+ text_window_clear();
+
if (key == KEY_ESCAPE) finish(0);
if (key == KEY_RESIZE) {
@@ -80,7 +86,6 @@
clear();
refresh();
- text_window_resize();
}
if (key == KEY_DOWN) {
@@ -90,12 +95,32 @@
}
if (counter == 2) {
- text_window_out("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmoLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmo");
+ text_window_out("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. ");
}
if (counter == 3) {
- text_window_out("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmoLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmoLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmo");
+ text_window_out("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd guber");
counter = 0;
}
}
+
+ map[80*20] = '\n';
+
+ if (key == KEY_LEFT) {
+ for (int i = 0; i < 80*20; i++) {
+ map[i] = 'o';
+ }
+ map_window_update(map);
+ }
+
+ if (key == KEY_RIGHT) {
+ for (int i = 0; i < 80*20; i++) {
+ map[i] = '*';
+ }
+ map_window_update(map);
+ }
+
+ stat_window_update(stats);
+
+ doupdate();
}
Added: trunk/map_window.c
===================================================================
--- trunk/map_window.c (rev 0)
+++ trunk/map_window.c 2008-11-22 12:30:44 UTC (rev 49)
@@ -0,0 +1,53 @@
+#include "map_window.h"
+
+#include <string.h>
+#include <ncurses.h>
+
+#define MAPWND_MAXWIDTH (80)
+#define MAPWND_MAXHEIGHT (20)
+#define MAPWND_SCREENSIZE (MAPWND_MAXWIDTH * MAPWND_MAXHEIGHT)
+
+static WINDOW *border_wnd; /* Window handle */
+
+
+
+void map_window_create(void)
+{
+ border_wnd = newwin(MAPWND_MAXHEIGHT, MAPWND_MAXWIDTH, 2, 0);
+
+ if (!border_wnd) {
+ fprintf(stderr, "map window creation failed!\n");
+ return;
+ }
+
+ wattron(border_wnd, COLOR_PAIR(4));
+ wbkgd(border_wnd, COLOR_PAIR(4));
+
+ wrefresh(border_wnd);
+}
+
+
+
+void map_window_destroy()
+{
+ if (border_wnd) {
+ /* Free resources consumed by the window */
+ delwin(border_wnd);
+ border_wnd = NULL;
+ }
+}
+
+
+
+void map_window_update(const chtype *map)
+{
+ /* Set cursor to first position (upper left) in map window */
+ wmove(border_wnd, 0, 0);
+
+ /* Copy map to local map */
+ for (int s = 0; s < MAPWND_SCREENSIZE; s++) {
+ waddch(border_wnd, map[s]);
+ }
+
+ wnoutrefresh(border_wnd);
+}
Property changes on: trunk/map_window.c
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
Added: trunk/map_window.h
===================================================================
--- trunk/map_window.h (rev 0)
+++ trunk/map_window.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -0,0 +1,10 @@
+#ifndef DERACIEL_MAP_WINDOW_H
+#define DERACIEL_MAP_WINDOW_H
+
+#include <ncurses.h>
+
+void map_window_create(void);
+void map_window_destroy(void);
+void map_window_update(const chtype *m);
+
+#endif // !DERACIEL_MAP_WINDOW_H
Property changes on: trunk/map_window.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
Added: trunk/stat.h
===================================================================
--- trunk/stat.h (rev 0)
+++ trunk/stat.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -0,0 +1,27 @@
+#ifndef DERACIEL_STAT_H
+#define DERACIEL_STAT_H
+
+#include <stdint.h>
+
+typedef uint32_t stat_t;
+
+enum {
+ STAT_CUR_HITPOINTS,
+ STAT_MAX_HITPOINTS,
+ STAT_CUR_MAGIC,
+ STAT_MAX_MAGIC,
+ STAT_STRENGTH,
+ STAT_DEXTERITY,
+ STAT_WISDOM,
+ STAT_WILLPOWER,
+ STAT_CONSTITUTION,
+ STAT_CHARISMA,
+ STAT_XP_LEVEL,
+ STAT_EXPERIENCE,
+ STAT_SCORE,
+ STAT_GOLD,
+ STAT_TURNS,
+ STAT_MAX_STATS
+};
+
+#endif // !DERACIEL_STAT_H
Property changes on: trunk/stat.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
Added: trunk/stat_window.c
===================================================================
--- trunk/stat_window.c (rev 0)
+++ trunk/stat_window.c 2008-11-22 12:30:44 UTC (rev 49)
@@ -0,0 +1,76 @@
+#include "stat_window.h"
+
+#include <string.h>
+#include <ncurses.h>
+
+#define BORDER_WND_MAXWIDTH (80)
+#define BORDER_WND_MAXHEIGHT (3)
+
+#define STATWINDOW_BORDER
+
+static WINDOW *border_wnd; /* Window handle */
+
+static void draw_border();
+
+
+void stat_window_create(void)
+{
+ border_wnd = newwin(BORDER_WND_MAXHEIGHT, BORDER_WND_MAXWIDTH, 25-BORDER_WND_MAXHEIGHT, 0);
+
+ if (!border_wnd) {
+ fprintf(stderr, "stat window creation failed!\n");
+ return;
+ }
+
+ wattron(border_wnd, COLOR_PAIR(3));
+ wbkgd(border_wnd, COLOR_PAIR(3));
+
+#ifdef STATWINDOW_BORDER
+ /* Redraw border with new dimensions */
+ draw_border();
+#endif // !STATWINDOW_BORDER
+
+ wrefresh(border_wnd);
+}
+
+
+
+void stat_window_destroy()
+{
+ if (border_wnd) {
+ /* Free resources consumed by the window */
+ delwin(border_wnd);
+ border_wnd = NULL;
+ }
+}
+
+
+
+void draw_border()
+{
+ wborder(border_wnd, 0, 0, 0, ' ', '/', '\\', ACS_VLINE, ACS_VLINE);
+}
+
+
+
+void stat_window_update(const stat_t *stats)
+{
+ /* Clear window from screen */
+ wclear(border_wnd);
+
+#ifdef STATWINDOW_BORDER
+ /* Redraw border with new dimensions */
+ draw_border();
+#endif // !STATWINDOW_BORDER
+
+ mvwprintw(border_wnd, 1, 2, "$:%d HP:%d(%d) MP:%d(%d) St:%2d Dx:%2d Co:%2d Wi:%2d Wp:%2d Ch:%2d",
+ stats[STAT_GOLD],
+ stats[STAT_CUR_HITPOINTS], stats[STAT_MAX_HITPOINTS], stats[STAT_CUR_MAGIC], stats[STAT_MAX_MAGIC],
+ stats[STAT_STRENGTH], stats[STAT_DEXTERITY], stats[STAT_CONSTITUTION],
+ stats[STAT_WISDOM], stats[STAT_WILLPOWER], stats[STAT_CHARISMA]);
+
+ mvwprintw(border_wnd, 2, 2, "Xp:%2d/%d T:%d S:%d",
+ stats[STAT_XP_LEVEL], stats[STAT_EXPERIENCE], stats[STAT_SCORE], stats[STAT_TURNS]);
+
+ wnoutrefresh(border_wnd);
+}
Property changes on: trunk/stat_window.c
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
Added: trunk/stat_window.h
===================================================================
--- trunk/stat_window.h (rev 0)
+++ trunk/stat_window.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -0,0 +1,11 @@
+#ifndef DERACIEL_STAT_WINDOW_H
+#define DERACIEL_STAT_WINDOW_H
+
+#include "stat.h"
+
+void stat_window_create(void);
+void stat_window_destroy(void);
+void stat_window_update(const stat_t *stats);
+
+#endif // !DERACIEL_STAT_WINDOW_H
+
Property changes on: trunk/stat_window.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
Modified: trunk/text_window.c
===================================================================
--- trunk/text_window.c 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/text_window.c 2008-11-22 12:30:44 UTC (rev 49)
@@ -1,18 +1,15 @@
#include "text_window.h"
#include <string.h>
-#include <form.h>
+#include <ncurses.h>
#define BORDER_WND_MAXWIDTH (80)
#define BORDER_WND_MAXHEIGHT (2)
-#define TEXTWINDOW_LINEBUFFER_SIZE (3*78 + 1) // 3 lines * 78 characters + '\n'
#define TEXTWINDOW_BORDER
static WINDOW *border_wnd; /* Window handle */
static WINDOW *text_wnd; /* Window handle */
-static char linebuffer[TEXTWINDOW_LINEBUFFER_SIZE];
-static void repaint(void);
static void draw_border();
@@ -27,9 +24,15 @@
}
wattron(border_wnd, COLOR_PAIR(1));
- wbkgd(text_wnd, COLOR_PAIR(2));
+ wbkgd(text_wnd, COLOR_PAIR(1));
- repaint();
+#ifdef TEXTWINDOW_BORDER
+ /* Redraw border with new dimensions */
+ draw_border();
+#endif // !TEXTWINDOW_BORDER
+
+ wrefresh(border_wnd);
+ wrefresh(text_wnd);
}
@@ -37,10 +40,6 @@
void text_window_destroy()
{
if (border_wnd) {
- /* Clear window from screen */
- wclear(border_wnd);
- wrefresh(border_wnd);
-
/* Free resources consumed by the window */
delwin(text_wnd);
text_wnd = NULL;
@@ -52,64 +51,40 @@
-void text_window_resize(void)
+void text_window_out(const char *string)
{
- /* Clear window from screen */
wclear(border_wnd);
wclear(text_wnd);
- /* Reallocate storage for resized text window */
- wresize(border_wnd, BORDER_WND_MAXHEIGHT, BORDER_WND_MAXWIDTH);
- wresize(text_wnd, BORDER_WND_MAXHEIGHT-1, BORDER_WND_MAXWIDTH-2);
+#ifdef TEXTWINDOW_BORDER
+ /* Redraw border with new dimensions */
+ draw_border();
+#endif // !TEXTWINDOW_BORDER
- repaint();
-}
+ wmove(text_wnd, 0, 0);
+ wprintw(text_wnd, string);
-
-
-void text_window_out(const char *string)
-{
- strncpy(linebuffer, string, TEXTWINDOW_LINEBUFFER_SIZE);
- linebuffer[TEXTWINDOW_LINEBUFFER_SIZE-1] = '\0';
-
- clear();
- refresh();
-
- // mod will be 0 if buffer fits *exactly* into line and 1 if we have to add a new line
- int mod = strlen(linebuffer) % (BORDER_WND_MAXWIDTH-2);
-
- int lines = (mod == 0 ? 0 : 1) + strlen(linebuffer)/(BORDER_WND_MAXWIDTH-2);
-
- wresize(text_wnd, lines, BORDER_WND_MAXWIDTH-2);
- wresize(border_wnd, lines+1, BORDER_WND_MAXWIDTH);
-
- repaint();
+ wrefresh(border_wnd);
+ wrefresh(text_wnd);
}
-
-void draw_border()
+void text_window_clear()
{
- wborder(border_wnd, 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, '\\', '/');
-}
-
-
-
-void repaint()
-{
- /* Clear window from screen */
wclear(border_wnd);
wclear(text_wnd);
#ifdef TEXTWINDOW_BORDER
/* Redraw border with new dimensions */
draw_border();
-#endif // TEXTWINDOW_BORDER
+#endif // !TEXTWINDOW_BORDER
- wmove(text_wnd, 0, 0);
- wprintw(text_wnd, linebuffer);
-
wnoutrefresh(border_wnd);
wnoutrefresh(text_wnd);
- doupdate();
}
+
+
+void draw_border()
+{
+ wborder(border_wnd, 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, '\\', '/');
+}
Modified: trunk/text_window.h
===================================================================
--- trunk/text_window.h 2008-11-22 01:54:53 UTC (rev 48)
+++ trunk/text_window.h 2008-11-22 12:30:44 UTC (rev 49)
@@ -1,12 +1,10 @@
-#ifndef _TEXT_WINDOW_H
-#define _TEXT_WINDOW_H
+#ifndef DERACIEL_TEXT_WINDOW_H
+#define DERACIEL_TEXT_WINDOW_H
-#include <curses.h>
-
void text_window_create(void);
void text_window_destroy(void);
-void text_window_resize(void);
void text_window_out(const char *string);
+void text_window_clear(void);
-#endif // _TEXT_WINDOW_H
+#endif // !DERACIEL_TEXT_WINDOW_H
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|