Bonsoir à tous !
Alors voila, je travaille sur un projet visant à réaliser un forum en C + SQL.
Pour l'instant je m'attaque surtout à la partie graphique et j'ai décidé d'utiliser la très sympathique SDL.
Rien de bien compliqué jusqu'à ce que je m'attaque à un point important pour créer un forum : rentrer des chaines de caractères.
D'après ce que j'ai compris il n'existe pas en SDL de fonctions toute faite pour simuler un "scanf".
Bref, en m'inspirant de ce que j'ai pu trouver sur le net (dont certains guides était en C++), j'ai essayé de rédiger une fonction qui se charge de prendre la saisie de l'utilisateur, voici la partie importante du code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> #include <SDL/SDL_image.h> #include "param.h" typedef struct StringInput { //The storage string char* str; //The text surface SDL_Surface *text; } SDL_StringInput; SDL_StringInput handle_input(SDL_StringInput textinput,SDL_Event event,TTF_Font *police,SDL_Color couleur) { //If a key was pressed if( event.type == SDL_KEYDOWN ) { //Keep a copy of the current version of the string char* temp = textinput.str; //If the string less than maximum size if( strlen(textinput.str) <= 16 ) { //If the key is a space if( event.key.keysym.unicode == (Uint16)' ' ) { //Append the character textinput.str += (char)event.key.keysym.unicode; } //If the key is a number else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) ) { //Append the character textinput.str += (char)event.key.keysym.unicode; } //If the key is a uppercase letter else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) ) { //Append the character textinput.str += (char)event.key.keysym.unicode; } //If the key is a lowercase letter else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) ) { //Append the character textinput.str += (char)event.key.keysym.unicode; } } //If backspace was pressed and the string isn't blank if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( strlen(textinput.str) != 0 ) ) { //Remove a character from the end textinput.str[strlen(textinput.str)-1] =0; } //If the string was changed if( textinput.str != temp ) { //Free the old surface //SDL_FreeSurface( textinput.text ); //Render a new text surface textinput.text = TTF_RenderText_Blended( police, textinput.str, couleur ); } } return textinput; }
Déjà là, je suis persuadé qu'il a plein d'erreur, étant donné que je me suis inspiré fortement d'un guide en C++, j'ai donc essayé de traduire après avoir plus ou moins bien compris le code.
(si vous voulez voir le code originel http://lazyfoo.net/SDL_tutorials/lesson23/index.php)
Voila, si vous pouviez me donner un coup de pouce, juger mon code je dirais pas non !
En plus ce n'est qu'une fonction parmi d'autre, je ne sais pas trop comment procéder pour que mon texte s'affiche pendant que je le tape par exemple.
Merci d'avance, cordialement.
Partager