create a new level structure, that makes the code more maintenable. Move level.*...
[neverball] / ball / set.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "glext.h"
19 #include "config.h"
20 #include "image.h"
21 #include "levels.h"
22 #include "set.h"
23
24 /*---------------------------------------------------------------------------*/
25
26 struct set
27 {
28     char init_levels[MAXSTR];
29     char init_scores[MAXSTR];
30     char user_scores[MAXSTR];
31
32     char shot[MAXSTR];
33     char name[MAXSTR];
34     char desc[MAXSTR];
35 };
36
37 static int set_state = 0;
38
39 static int set;
40 static int count;
41
42 static struct set set_v[MAXSET];
43
44 /*---------------------------------------------------------------------------*/
45
46 void set_init()
47 {
48     FILE *fin;
49
50     if (set_state)
51         set_free();
52
53     count = 0;
54
55     if ((fin = fopen(config_data(SET_FILE), "r")))
56     {
57         while (fscanf(fin, "%s %s %s %s\n",
58                       set_v[count].init_levels,
59                       set_v[count].init_scores,
60                       set_v[count].user_scores,
61                       set_v[count].shot) == 4 &&
62                fgets(set_v[count].name, MAXSTR, fin) &&
63                fgets(set_v[count].desc, MAXSTR, fin))
64         {
65             char *p = set_v[count].name + strlen(set_v[count].name) - 1;
66             char *q = set_v[count].desc + strlen(set_v[count].desc) - 1;
67
68             if (*p == '\n') *p = 0;
69             if (*q == '\n') *q = 0;
70
71             count++;
72         }
73
74         fclose(fin);
75
76         set_state = 1;
77     }
78 }
79
80 int  set_exists(int i)
81 {
82     return (0 <= i && i < count);
83 }
84
85 void set_goto(int i)
86 {
87     level_init(set_v[i].init_levels,
88                set_v[i].init_scores,
89                set_v[i].user_scores);
90     set = i;
91 }
92
93 int set_curr(void)
94 {
95     return set;
96 }
97
98 void set_free(void)
99 {
100     level_free();
101     set_state = 0;
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 const char *set_name(int i)
107 {
108     return set_exists(i) ? set_v[i].name : "";
109 }
110
111 const char *set_desc(int i)
112 {
113     return set_exists(i) ? set_v[i].desc : "";
114 }
115
116 const char *set_shot(int i)
117 {
118     return set_exists(i) ? set_v[i].shot :  set_v[0].shot;
119 }
120
121 /*---------------------------------------------------------------------------*/