Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [a5] Anything wrong with the code?

This thread is locked; no one can reply to it. rss feed Print
[a5] Anything wrong with the code?
Jefferson Almeida
Member #12,659
March 2011

Anything wrong in code?

#SelectExpand
1 2int get_ascii_value(int keycode, int shift_key) { 3 if(keycode >= ALLEGRO_KEY_A && keycode <= ALLEGRO_KEY_Z) { 4 printf(" returning %d.\n", (keycode - ALLEGRO_KEY_A + 97) - shift_key ? 32 : 0); 5 return (keycode - ALLEGRO_KEY_A + 97) - (shift_key ? 32 : 0); 6 } 7 if(keycode >= ALLEGRO_KEY_0 && keycode <= ALLEGRO_KEY_9) { 8 return keycode - ALLEGRO_KEY_0 + 48; 9 } 10 if(keycode == ALLEGRO_KEY_MINUS) { 11 if(shift_key) return '_'; 12 else 13 return '-'; 14 } 15} 16 17void test() { 18 int get_filename_screen; 19 bool quit; 20 ALLEGRO_DISPLAY *display; 21 ALLEGRO_EVENT_QUEUE *q; 22 char *filename; 23 ALLEGRO_EVENT event; 24 int current_character = 0; 25 int string[MAX_LENGTH_FOR_FRD_STRING]; 26 al_flush_event_queue(q); 27 int key; 28 int shift_key_pressed; 29 int ascii_value; 30 if(filename[0] != '\0') { 31 printf(" get_filename_screen() : string not zeroed.\n"); 32 //return QUIT_DUE_TO_PROBLEM; 33 } 34 strcat(filename, ".map"); 35 36 do { 37 if(!al_is_event_queue_empty(q)) { 38 while(al_get_next_event(q, &event)) { 39 switch(event.type) { 40 case ALLEGRO_EVENT_DISPLAY_CLOSE: 41 quit = true; 42 break; 43 case ALLEGRO_EVENT_KEY_CHAR: 44 key = event.keyboard.keycode; 45 shift_key_pressed = (event.keyboard.modifiers & ALLEGRO_KEYMOD_SHIFT) ? 1 : 0; 46 if(key == ALLEGRO_KEY_ESCAPE) { 47 quit = true; 48 } 49 else if(key == ALLEGRO_KEY_BACKSPACE) { 50 if(current_character > 0) { 51 filename[current_character - 1] = 0; 52 current_character -= 1; 53 strcat( filename, ".map"); 54 } 55 } 56 else if( current_character < MAX_LENGTH_FOR_FRD_STRING - 5 && (ascii_value = get_ascii_value(key, shift_key_pressed)) != -1) { 57 //printf(" here %d \n", (int) ascii_value); 58 filename[ current_character++ ] = (char) ascii_value; 59 filename[current_character] = 0; 60 strcat(filename, ".map"); 61 } 62 else if(key == ALLEGRO_KEY_ENTER && current_character != 0) { 63 // return FILENAME_STRING_OKAY; 64 } 65 break; 66 } 67 } 68 } 69 al_clear_to_color(DARK_RED); 70 71 al_draw_text(font, WHITE, 100, 100, 0, "Enter a name for the file."); 72 al_draw_textf(font, WHITE, 100, 140, 0, "Name for file : %s", filename); 73 al_draw_text(font, WHITE, 100, 200, 0, "Press ENTER when filename is finished."); 74 75 al_flip_display(); 76 77 } while(1); 78 79}

Edgar Reynaldo
Member #8,592
May 2007
avatar

That's kind of a broad statement. What is it you want the code to do, and what is it the code is not doing?

There's actually a lot wrong with that code :
1a) You call al_flush_event_queue on an uninitialized event queue 'q'.
1b) You query (call al_get_next_event on) an uninitialized event queue 'q'.
2) Your indentation is still wrong - 3 spaces per indentation level is usually agreeable with most people.
3) You never allocate memory for 'filename'.
4) You never use the variable 'string'.
5) get_ascii_value doesn't return any value if the conditions aren't met.

Isso é uma espécie de afirmação abrangente. O que é que você quer o código para fazer, e qual é o código não está fazendo?

Na verdade, há muita coisa errada com este código:
1a) Você chama al_flush_event_queue em 'q' uma fila de eventos não inicializado.
1b) Você al_get_next_event (consulta chamar) 'q' uma fila de eventos não inicializado.
2) O recuo ainda é errado - 3 vagas por nível de recuo é geralmente agradável, com a maioria das pessoas.
3) Você nunca alocar memória para 'filename'.
4) Você nunca usa 'string' variável.
5) get_ascii_value não retorna nenhum valor se as condições não são cumpridas.

Jefferson Almeida
Member #12,659
March 2011

To the person can type a text on the screen and you can save the file with the name typed

The recue is the space of "}" ?

I'll try to fix errors! Thanks!

Edgar Reynaldo
Member #8,592
May 2007
avatar

To the person can type a text on the screen and you can save the file with the name typed

I actually understand what you are trying to do with the code. Try to fix the errors I mentioned, and then tell me what is not working.

Eu realmente entendo o que você está tentando fazer com o código. Tentar corrigir os erros que eu mencionei, e depois me diga o que não está funcionando.

Jefferson Almeida said:

The recue is the space of "}" ?

This did not translate well. I don't understand what you are saying. Use Google translate to translate it from portugese to english for me.

Isso não traduzem bem. Eu não entendo o que você está dizendo. Use o Google translate para traduzir de português para Inglês para mim.

Jefferson Almeida
Member #12,659
March 2011

You say: 2) Your indentation is still wrong - 3 spaces per indentation level is usually agreeable with most people.

The indentation that you mention is related with the "}" of the code?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Yes, every time you use an if statement or else if statement or create a new block of code with a bracket ({) you should add an indentation level. Every time you close one of those statements you should remove an indentation level. Whether you use 2, 3, 4, or even 8 spaces for each level is up to you. Just be consistent :

Sim, toda vez que você usar uma instrução if ou então se ou criar um novo bloco de código com um suporte ({), você deve adicionar um nível de recuo. Toda vez que você fechar uma dessas demonstrações que você deve remover um nível de recuo. Se você usar 2, 3, 4 ou mesmo 8 espaços para cada nível é até você. Basta ser coerente:

#SelectExpand
1int main(int argc , char** argv) { 2 int a = 5; 3 int b = 2; 4 if (a > 2) { 5 while (1) { 6 ++a; 7 if (a > 10) { 8 switch (b) { 9 case 0 : 10 break; 11 case 1 : 12 break; 13 case 2 : 14 break; 15 } 16 break; 17 } 18 } 19 } 20 return 0; 21}

Your IDE should do all this for you when you press tab. What IDE are you using? Code::Blocks?

O IDE deve fazer tudo isso para você quando você pressiona tab. Qual IDE você está usando? Code:: Blocks?

Jefferson Almeida
Member #12,659
March 2011

I understand now... Yeah I'm using code::blocks :)

Edgar Reynaldo
Member #8,592
May 2007
avatar

Jefferson Almeida
Member #12,659
March 2011

al_flush_event_queue(event_queue);
(Remove events of the queue!)

if(al_is_event_queue_empty(event_queue)) {
(If queue specified is currently empty, You have nothing typed yet, so is empty?)

if(al_get_next_event(event_queue, &ret_event)) {
(To get the next event, typed text or something else.)

This line of reasoning is correct?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Jefferson Almeida
Member #12,659
March 2011

EDIT 1: I tried but could not develop the function to write on the screen, but practicing with the code below I managed to make the file that is loaded into the console open on one screen with the display function, I'll try to draw something to discover out if it really worked.

hun... thanks!

To run the code in the console ... I got more or less what I want!
Look!

#SelectExpand
1 2#include <stdio.h> 3#include <stdlib.h> 4#include <allegro5/allegro.h> 5#include <string> 6#include <strings.h> 7 8using namespace std; 9 10// Number of "layers" that will use the map 11int l_size; 12// player position X and Y 13int p_x = 0, p_y = 0; 14int m_siz = 15; 15int map[3][50][50]; 16 17// Struct to map layers 18struct no 19{ 20 ALLEGRO_BITMAP *layer; 21}; 22struct no layers[100]; 23 24// Map Info 25char map_name[20]; 26char directory[50]; 27 28// Struct to salve the map 29struct mapdata 30{ 31 int mapdat[3][50][50]; 32 int m_size; 33 char name[20]; 34 struct no m_layers[100]; 35}; 36 37struct mapdata savemap; 38 39void test(); 40 41int main(int argc , char** argv) { 42 test(); 43} 44 45void test() { 46 bool go = false; 47 FILE *arq; 48 printf("Isometric Map Editor\n"); 49 printf("Select\n"); 50 printf("[1] New Map\n"); 51 printf("[2] Edit Map\n"); 52 int resp; 53 scanf("%d",&resp); 54 if(resp==1) { 55 while (go == false) { 56 printf("Enter a name for your map (max 20)\n"); 57 fflush(stdin); 58 gets(map_name); 59 printf("Enter a size N for your map ( N x N ) max 50\n"); 60 fflush(stdin); 61 scanf("%d",&resp); 62 if(resp <50 && resp>0) m_siz = resp; 63 else printf("Can not create a map size %d, therefore I will create (15 x 15)\n", resp); 64 strcpy(directory, "data/map/"); 65 strcat(directory, map_name); 66 strcat(directory, ".map"); 67 printf("creating: %s\n", directory); 68 69 if(!fopen(directory,"r")) { 70 arq=fopen(directory,"w+"); 71 go = true; 72 printf("Map created\n"); 73 system("pause"); 74 fclose(arq); 75 } 76 else printf("Error!(Try other name)\n"); 77 } 78 } 79 else if(resp==2) { 80 printf("Type the name of your map\n"); 81 fflush(stdin); 82 gets(map_name); 83 strcpy(directory, "data/map/"); 84 strcat(directory, map_name); 85 strcat(directory, ".map"); 86 printf("\n opening file '%s'\n", directory); 87 if(fopen(directory, "r")) { 88 arq = fopen(directory, "r"); 89 fread(&savemap, sizeof(struct mapdata), 1, arq); 90 for(int z = 0 ; z < 3; z++) { 91 for(int y = 0 ; y < m_siz ; y++) { 92 for(int x = 0 ; x < m_siz ; x++) { 93 map[z][x][y] = savemap.mapdat[z][x][y]; 94 } 95 } 96 } 97 strcpy(map_name, savemap.name); 98 m_siz = savemap.m_size; 99 for(int c_layer=0;c_layer< l_size;c_layer++) { 100 layers[c_layer].layer = savemap.m_layers[c_layer].layer; 101 } 102 fclose(arq); 103 printf("File opening... !\n");system("pause"); 104 } 105 else 106 printf("Error... Could not find on the map %s\n", map_name); 107 } 108};

Edgar Reynaldo
Member #8,592
May 2007
avatar

EDIT 1: I tried but could not develop the function to write on the screen, but practicing with the code below I managed to make the file that is loaded into the console open on one screen with the display function, I'll try to draw something to discover out if it really worked.

I'm not sure what you're asking. What do you mean 'write on the screen'?

Jefferson Almeida
Member #12,659
March 2011

The things that I type in keyboard appear on the screen. :P

J-Gamer
Member #12,491
January 2011
avatar

You need to look for CHAR events to do this.
link

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Jefferson Almeida
Member #12,659
March 2011

@@"

Edgar Reynaldo
Member #8,592
May 2007
avatar

Jefferson Almeida
Member #12,659
March 2011

It's no possible to type

Edgar Reynaldo
Member #8,592
May 2007
avatar

Go to: