Atrinik Client  4.0
game_news.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <global.h>
33 #include <toolkit/string.h>
34 #include <toolkit/curl.h>
35 #include <curl/curl.h>
36 
40 #define NEWS_MAX_WIDTH 455
41 
44 #define NEWS_MAX_HEIGHT 250
45 
48 #define NEWS_FONT FONT_SANS12
49 
53 typedef struct game_news {
57  char *title;
58 
62  curl_request_t *request;
63 
67  curl_state_t state;
68 
72  char *msg;
73 
78 
82  uint32_t scroll_offset;
83 
87  uint32_t num_lines;
88 } game_news_t;
89 
91 static int
93 {
95 
96  curl_state_t state = curl_request_get_state(game_news->request);
97  if (game_news->state != state) {
98  game_news->state = state;
99  popup->redraw = 1;
100  }
101 
102  if (!popup->redraw) {
103  return 1;
104  }
105 
106  popup->redraw = 0;
107  surface_show(popup->surface, 0, 0, NULL, texture_surface(popup->texture));
108 
109  SDL_Rect box;
110  box.w = popup->surface->w;
111  box.h = 38;
112  text_show(popup->surface, FONT_SERIF16, game_news->title, 0, 0,
114 
115  box.w = popup->surface->w;
116  box.h = popup->surface->h - box.h;
117 
118  if (state == CURL_STATE_ERROR) {
119  text_show(popup->surface, FONT_SERIF12, "Connection timed out.", 0, 0,
121  return 1;
122  } else if (state == CURL_STATE_INPROGRESS) {
123  text_show(popup->surface, FONT_SERIF12,
124  "Downloading news, please wait...", 0, 0, COLOR_WHITE,
126  return 1;
127  } else if (state == CURL_STATE_OK) {
128  if (game_news->msg == NULL) {
129  char *body = curl_request_get_body(game_news->request, NULL);
130  game_news->msg = estrdup(body != NULL ? body : "???");
131 
132  box.w = NEWS_MAX_WIDTH;
133  box.h = NEWS_MAX_HEIGHT;
134  text_show(NULL, NEWS_FONT, game_news->msg, 10, 40, COLOR_WHITE,
136  game_news->num_lines = box.h;
137  scrollbar_create(&game_news->scrollbar, 15, 240,
138  &game_news->scroll_offset, &game_news->num_lines,
139  box.y);
140  box.h = NEWS_MAX_HEIGHT;
141  }
142  }
143 
144  box.w = NEWS_MAX_WIDTH;
145  box.h = NEWS_MAX_HEIGHT;
146  box.y = game_news->scroll_offset;
147  text_show(popup->surface, NEWS_FONT, game_news->msg, 10, 40, COLOR_WHITE,
149 
150  game_news->scrollbar.px = popup->x;
151  game_news->scrollbar.py = popup->y;
152  scrollbar_show(&game_news->scrollbar, popup->surface,
153  popup->surface->w - 28, 45);
154  return 1;
155 }
156 
158 static int
159 popup_event (popup_struct *popup, SDL_Event *event)
160 {
162 
163  if (game_news->msg == NULL) {
164  return -1;
165  }
166 
167  if (scrollbar_event(&game_news->scrollbar, event)) {
168  popup->redraw = 1;
169  return 1;
170  }
171 
172  if (event->type == SDL_KEYDOWN) {
173  /* Scroll the text. */
174  if (event->key.keysym.sym == SDLK_DOWN) {
175  scrollbar_scroll_adjust(&game_news->scrollbar, 1);
176  popup->redraw = 1;
177  return 1;
178  } else if (event->key.keysym.sym == SDLK_UP) {
179  scrollbar_scroll_adjust(&game_news->scrollbar, -1);
180  popup->redraw = 1;
181  return 1;
182  } else if (event->key.keysym.sym == SDLK_PAGEUP) {
184  -game_news->scrollbar.max_lines);
185  popup->redraw = 1;
186  return 1;
187  } else if (event->key.keysym.sym == SDLK_PAGEDOWN) {
189  game_news->scrollbar.max_lines);
190  popup->redraw = 1;
191  return 1;
192  }
193  }/* Mouse wheel? */
194  else if (event->type == SDL_MOUSEBUTTONDOWN) {
195  if (event->button.button == SDL_BUTTON_WHEELDOWN) {
196  scrollbar_scroll_adjust(&game_news->scrollbar, 1);
197  popup->redraw = 1;
198  return 1;
199  } else if (event->button.button == SDL_BUTTON_WHEELUP) {
200  scrollbar_scroll_adjust(&game_news->scrollbar, -1);
201  popup->redraw = 1;
202  return 1;
203  }
204  }
205 
206  return -1;
207 }
208 
210 static int
212 {
214  efree(game_news->title);
215  curl_request_free(game_news->request);
216 
217  if (game_news->msg != NULL) {
218  efree(game_news->msg);
219  }
220 
221  return 1;
222 }
223 
230 void
231 game_news_open (const char *title)
232 {
233  popup_struct *popup = popup_create(texture_get(TEXTURE_TYPE_CLIENT,
234  "popup"));
235  popup->draw_func = popup_draw;
236  popup->event_func = popup_event;
238  popup->disable_texture_drawing = 1;
239 
240  game_news_t *game_news = ecalloc(1, sizeof(*game_news));
241  popup->custom_data = game_news;
242  game_news->title = estrdup(title);
243 
244  /* Initialize cURL, escape the game news title and construct
245  * the url to use for downloading. */
246  CURL *curl = curl_easy_init();
247  char *news_id = curl_easy_escape(curl, title, 0);
248  char url[HUGE_BUF];
249  snprintf(VS(url), "%s?news=%s", clioption_settings.game_news_url, news_id);
250  curl_free(news_id);
251  curl_easy_cleanup(curl);
252 
253  /* Start downloading. */
254  game_news->request = curl_request_create(url, CURL_PKEY_TRUST_ULTIMATE);
255  curl_request_start_get(game_news->request);
256  game_news->state = CURL_STATE_NONE;
257 }
uint32_t num_lines
Definition: game_news.c:87
#define TEXT_WORD_WRAP
Definition: text.h:226
int(* event_func)(struct popup_struct *popup, SDL_Event *event)
Definition: popup.h:148
curl_request_t * request
Definition: game_news.c:62
int(* draw_func)(struct popup_struct *popup)
Definition: popup.h:127
#define TEXT_LINES_SKIP
Definition: text.h:263
SDL_Surface * texture_surface(texture_struct *texture)
Definition: texture.c:303
int(* destroy_callback_func)(struct popup_struct *popup)
Definition: popup.h:159
void * custom_data
Definition: popup.h:78
char * msg
Definition: game_news.c:72
struct game_news game_news_t
void text_show(SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box)
Definition: text.c:1983
int x
Definition: popup.h:87
#define COLOR_HGOLD
Definition: text.h:319
texture_struct * texture_get(texture_type_t type, const char *name)
Definition: texture.c:279
static int popup_destroy_callback(popup_struct *popup)
Definition: game_news.c:211
#define TEXT_MARKUP
Definition: text.h:224
uint32_t max_lines
Definition: scrollbar.h:77
#define TEXT_ALIGN_CENTER
Definition: text.h:230
void game_news_open(const char *title)
Definition: game_news.c:231
void scrollbar_show(scrollbar_struct *scrollbar, SDL_Surface *surface, int x, int y)
Definition: scrollbar.c:443
static int popup_event(popup_struct *popup, SDL_Event *event)
Definition: game_news.c:159
uint8_t redraw
Definition: popup.h:114
curl_state_t state
Definition: game_news.c:67
static int popup_draw(popup_struct *popup)
Definition: game_news.c:92
#define NEWS_MAX_HEIGHT
Definition: game_news.c:44
void scrollbar_scroll_adjust(scrollbar_struct *scrollbar, int adjust)
Definition: scrollbar.c:427
void scrollbar_create(scrollbar_struct *scrollbar, int w, int h, uint32_t *scroll_offset, uint32_t *num_lines, uint32_t max_lines)
Definition: scrollbar.c:350
#define NEWS_MAX_WIDTH
Definition: game_news.c:40
int y
Definition: popup.h:90
scrollbar_struct scrollbar
Definition: game_news.c:77
uint32_t scroll_offset
Definition: game_news.c:82
clioption_settings_struct clioption_settings
Definition: main.c:87
void surface_show(SDL_Surface *surface, int x, int y, SDL_Rect *srcrect, SDL_Surface *src)
Definition: sprite.c:761
int scrollbar_event(scrollbar_struct *scrollbar, SDL_Event *event)
Definition: scrollbar.c:529
#define TEXT_LINES_CALC
Definition: text.h:259
SDL_Surface * surface
Definition: popup.h:65
#define NEWS_FONT
Definition: game_news.c:48
char * title
Definition: game_news.c:57
#define TEXT_VALIGN_CENTER
Definition: text.h:246
texture_struct * texture
Definition: popup.h:70
uint8_t disable_texture_drawing
Definition: popup.h:75
#define COLOR_WHITE
Definition: text.h:289