- moved tux completly to sprites
[supertux.git] / src / type.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include "SDL_image.h"
22 #include "string.h"
23 #include "stdlib.h"
24 #include "setup.h"
25 #include "globals.h"
26 #include "screen.h"
27 #include "defines.h"
28 #include "type.h"
29 #include "scene.h"
30
31 void string_list_init(string_list_type* pstring_list)
32 {
33   pstring_list->num_items = 0;
34   pstring_list->active_item = -1;
35   pstring_list->item = NULL;
36 }
37
38 char* string_list_active(string_list_type* pstring_list)
39 {
40   if(pstring_list == NULL)
41     return "";
42
43   if(pstring_list->active_item != -1)
44     return pstring_list->item[pstring_list->active_item];
45   else
46     return "";
47 }
48
49 void string_list_add_item(string_list_type* pstring_list,const  char* str)
50 {
51   char *pnew_string;
52   pnew_string = (char*) malloc(sizeof(char)*(strlen(str)+1));
53   strcpy(pnew_string,str);
54   ++pstring_list->num_items;
55   pstring_list->item = (char**) realloc(pstring_list->item,sizeof(char**)*pstring_list->num_items);
56   pstring_list->item[pstring_list->num_items-1] = pnew_string;
57   if(pstring_list->active_item == -1)
58     pstring_list->active_item = 0;
59 }
60
61 void string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig)
62 {
63   int i;
64   string_list_free(pstring_list);
65   for(i = 0; i < pstring_list_orig.num_items; ++i)
66     string_list_add_item(pstring_list,pstring_list_orig.item[i]);
67 }
68
69 int string_list_find(string_list_type* pstring_list,const  char* str)
70 {
71   int i;
72   for(i = 0; i < pstring_list->num_items; ++i)
73     {
74       if(strcmp(pstring_list->item[i],str) == 0)
75         {
76           return i;
77         }
78     }
79   return -1;
80 }
81
82 void string_list_sort(string_list_type* pstring_list)
83 {
84   int i,j,y;
85
86   for(j = 0; j < pstring_list->num_items; ++j)
87     for(i = 0; i < pstring_list->num_items-1; ++i)
88       {
89
90         y = strcmp(pstring_list->item[i],pstring_list->item[i+1]);
91         if(y == 0)
92           {
93             continue;
94           }
95         else if(y < 0)
96           {
97             continue;
98           }
99         else if(y > 0)
100           {
101             char* char_pointer;
102             char_pointer = pstring_list->item[i];
103             pstring_list->item[i] = pstring_list->item[i+1];
104             pstring_list->item[i+1] = char_pointer;
105             continue;
106           }
107
108       }
109
110 }
111
112 void string_list_free(string_list_type* pstring_list)
113 {
114   if(pstring_list != NULL)
115     {
116       int i;
117       for(i=0; i < pstring_list->num_items; ++i)
118         free(pstring_list->item[i]);
119       free(pstring_list->item);
120       pstring_list->item = NULL;
121       pstring_list->num_items = 0;
122       pstring_list->active_item = -1;
123     }
124 }