summaryrefslogtreecommitdiff
path: root/src/screen/screen_table.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/screen/screen_table.c')
-rw-r--r--src/screen/screen_table.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/screen/screen_table.c b/src/screen/screen_table.c
new file mode 100644
index 0000000..b49de93
--- /dev/null
+++ b/src/screen/screen_table.c
@@ -0,0 +1,229 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "main.h"
+#include "list.h"
+#include "textFile.h"
+#include "homeDirector.h"
+
+#include "interface.h"
+#include "screen.h"
+#include "image.h"
+#include "hotKey.h"
+
+#ifndef NO_SOUND
+# include "music.h"
+#endif
+
+#include "screen_table.h"
+
+#include "widget_image.h"
+#include "widget_label.h"
+#include "widget_button.h"
+
+static widget_t *image_backgorund;
+static widget_t *button_back;
+
+static list_t *listWidgetLabelNumer;
+static list_t *listWidgetLabelName;
+static list_t *listWidgetLabelScore;
+
+static textFile_t *textFile;
+
+static void hotkey_escape()
+{
+ setScreen("mainMenu");
+}
+
+void startScreenTable()
+{
+#ifndef NO_SOUND
+ playMusic("menu", MUSIC_GROUP_BASE);
+#endif
+
+ registerHotKey(SDLK_ESCAPE, hotkey_escape);
+}
+
+void drawScreenTable()
+{
+ int i;
+
+ drawWidgetImage(image_backgorund);
+
+ drawWidgetButton(button_back);
+
+ for (i = 0; i < listWidgetLabelNumer->count; i++) {
+ widget_t *this;
+ this = (widget_t *) listWidgetLabelNumer->list[i];
+ drawWidgetLabel(this);
+ }
+
+ for (i = 0; i < listWidgetLabelName->count; i++) {
+ widget_t *this;
+ this = (widget_t *) listWidgetLabelName->list[i];
+ drawWidgetLabel(this);
+ }
+
+ for (i = 0; i < listWidgetLabelScore->count; i++) {
+ widget_t *this;
+ this = (widget_t *) listWidgetLabelScore->list[i];
+ drawWidgetLabel(this);
+ }
+}
+
+void eventScreenTable()
+{
+ eventWidgetButton(button_back);
+}
+
+void stopScreenTable()
+{
+ unregisterHotKey(SDLK_ESCAPE);
+}
+
+static void eventWidget(void *p)
+{
+ widget_t *button;
+
+ button = (widget_t *) (p);
+
+ if (button == button_back) {
+ setScreen("mainMenu");
+ }
+}
+
+static void setWidgetLabel()
+{
+ int i;
+
+ if (textFile == NULL) {
+ fprintf(stderr, _("Highscore file: \"%s\" was not loaded!"), SCREEN_TABLE_FILE_HIGHSCORE_NAME);
+
+ return;
+ }
+
+ if (listWidgetLabelNumer != NULL ||
+ listWidgetLabelName != NULL ||
+ listWidgetLabelScore != NULL) {
+ destroyListItem(listWidgetLabelNumer, destroyWidgetLabel);
+ destroyListItem(listWidgetLabelName, destroyWidgetLabel);
+ destroyListItem(listWidgetLabelScore, destroyWidgetLabel);
+ }
+
+ listWidgetLabelNumer = newList();
+ listWidgetLabelName = newList();
+ listWidgetLabelScore = newList();
+
+ for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) {
+ widget_t *label;
+ char name[STR_NAME_SIZE];
+ char score[STR_NUM_SIZE];
+ char num[STR_NUM_SIZE];
+ char *line;
+
+ line = (char *) textFile->text->list[i];
+
+ sscanf(line, "%s %s", name, score);
+ sprintf(num, "%2d)", i + 1);
+
+ label = newWidgetLabel(num, WINDOW_SIZE_X / 2 - 100, 200 + i * 20, WIDGET_LABEL_LEFT);
+ addList(listWidgetLabelNumer, label);
+
+ label = newWidgetLabel(name, WINDOW_SIZE_X / 2, 200 + i * 20, WIDGET_LABEL_CENTER);
+ addList(listWidgetLabelName, label);
+
+ label = newWidgetLabel(score, WINDOW_SIZE_X / 2 + 80, 200 + i * 20, WIDGET_LABEL_LEFT);
+ addList(listWidgetLabelScore, label);
+ }
+}
+
+static void loadHighscoreFile()
+{
+ char path[STR_PATH_SIZE];
+ int i;
+
+ sprintf(path, "%s/" SCREEN_TABLE_FILE_HIGHSCORE_NAME, getHomeDirector());
+ textFile = loadTextFile(path);
+
+ if (textFile == NULL) {
+ fprintf(stderr, _("I am unable to load: \"%s\"!\n"), path);
+ fprintf(stderr, _("Creating: \"%s\"\n"), path);
+ textFile = newTextFile(path);
+ } else {
+ return;
+ }
+
+ if (textFile == NULL) {
+ fprintf(stderr, _("I was unable to create: \"%s\"!\n"), path);
+ return;
+ }
+
+ for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) {
+ addList(textFile->text, strdup("no_name 0"));
+ }
+
+ saveTextFile(textFile);
+}
+
+int addPlayerInHighScore(char *name, int score)
+{
+ int i;
+
+ for (i = 0; i < SCREEN_TABLE_MAX_PLAYERS; i++) {
+ char *line;
+ char thisName[STR_NAME_SIZE];
+ int thisCore;
+
+ line = (char *) textFile->text->list[i];
+ sscanf(line, "%s %d", thisName, &thisCore);
+
+ if (score >= thisCore) {
+ char new[STR_SIZE];
+
+ sprintf(new, "%s %d", name, score);
+ insList(textFile->text, i, strdup(new));
+ delListItem(textFile->text, SCREEN_TABLE_MAX_PLAYERS, free);
+ setWidgetLabel();
+
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+void initScreenTable()
+{
+ image_t *image;
+
+ image = addImageData("screen_table.png", IMAGE_NO_ALPHA, "screen_table", IMAGE_GROUP_BASE);
+ image_backgorund = newWidgetImage(0, 0, image);
+
+ button_back = newWidgetButton(_("back"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2,
+ WINDOW_SIZE_Y - 100, eventWidget);
+
+ loadHighscoreFile();
+ listWidgetLabelNumer = NULL;
+ listWidgetLabelName = NULL;
+ listWidgetLabelScore = NULL;
+ setWidgetLabel();
+
+ registerScreen(newScreen("table", startScreenTable, eventScreenTable, drawScreenTable, stopScreenTable));
+}
+
+void quitScreenTable()
+{
+ destroyWidgetImage(image_backgorund);
+
+ destroyWidgetButton(button_back);
+ destroyListItem(listWidgetLabelNumer, destroyWidgetLabel);
+ destroyListItem(listWidgetLabelName, destroyWidgetLabel);
+ destroyListItem(listWidgetLabelScore, destroyWidgetLabel);
+
+ if (textFile != NULL) {
+ saveTextFile(textFile);
+ destroyTextFile(textFile);
+ }
+}