C - Character manipulation - Converting a char into an int

We can of course transform a char into an int with the atoi() function. But what we are going to do now it’s to create our own function to transform a string (several char) into a number. The string passed as the first argument will be our string. We are going to choose “140” for this example. To be sure we type a string, we had the double quotes around it. ...

April 10, 2012 · Mi-K

C - Keyword - Using extern

The extern keyword is designed to explicitely tells someone, being watching a variable in a code, that this variable has been firstly declared in another file. The extern keyword works also with C++. Let’s take an example. In the code below, I use 2 files. The first main.c where I declared the global_var variable. And file1.c where I redeclared it as an extern variable. So we can see that I can change the value of this variable from both files. ...

April 9, 2012 · Mi-K

C - SDL (Simple Directmedia Layer) - Creating a multicolored window

May you want to create a night club with the SDL library? It is maybe possible. With the code below of this tutorial, you can generate a different color every millisecond. So, why waiting? Let’s dance! #include <string.h> #include <errno.h> #include <stdlib.h> #include <dlfcn.h> #include <unistd.h> #include <sys/time.h> #include <SDL/SDL.h> typedef struct s_sdl { SDL_Surface *screen; SDL_Surface *rectangle; SDL_Rect *position; } t_sdl; int creation_rect(t_sdl *sdl, int r1, int r2, int r3) { sdl->rectangle = NULL; if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, r1, r2, r3)) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_Flip(sdl->screen) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } return (0); } int init(t_sdl *sdl) { int i = 0; if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { fprintf(stderr, "Error: %s\n", strerror(errno)); return (1); } if ((sdl->screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE)) == NULL) { fprintf(stderr, "Error: %s", SDL_GetError()); return (1); } SDL_WM_SetCaption("Night club! :D", NULL); if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, 255, 32, 45)) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_Flip(sdl->screen) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } while (i < 100) { creation_rect(sdl, my_random(), my_random(), my_random()); ++i; } SDL_FreeSurface(sdl->rectangle); return (0); } int my_random() { double seed; int fp; time_t t1; struct timeval tv; gettimeofday(&tv, NULL); seed = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000; srand(seed); usleep(10000); return (rand() % 255); } int main() { t_sdl sdl; sdl.screen = NULL; if (init(&sdl)) return (1); return (0); } Of course, we need to run this: ...

March 22, 2012 · Mi-K

C - SDL (Simple Directmedia Layer) - Creating a rectangle and moving it

We are going to see, with this tutorial, how to create a shape on a window and how to move it with keyboard arrows. We need, for this example, three files that we are going to create to dispatch the code: main.c manager_sdl.c h.h Of course, you can add a Makefile to easily compile and link files. But to link the .o, you need to add the -lSDL command. 1. main.c #include <string.h> #include <errno.h> #include <stdlib.h> #include <SDL/SDL.h> #include "h.h" int init(t_sdl *sdl) { if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { fprintf(stderr, "Error: %s\n", strerror(errno)); return (1); } if ((sdl->screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE)) == NULL) { fprintf(stderr, "Error: %s", SDL_GetError()); return (1); } SDL_WM_SetCaption("Hello World! :D", NULL); if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, 255, 32, 45)) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_Flip(sdl->screen) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } sdl->position->x = 0; sdl->position->y = 0; if (manager_event(sdl)) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } SDL_FreeSurface(sdl->rectangle); return (0); } int main () { t_sdl sdl; sdl.screen = NULL; if(init(&sdl)) return (1); return (0); } 2. manager_sdl.c #include <stdio.h> #include <string.h> #include <SDL/SDL.h> #include "h.h" int manager_event(t_sdl *sdl) { int gogogo; SDL_Event event; gogogo = 1; while (gogogo) { SDL_WaitEvent(&event); if (event.type == SDL_QUIT) gogogo = 0; else if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) { printf("ESC\n"); gogogo = 0; } if (event.key.keysym.sym == SDLK_RIGHT) { printf("RIGHT\n"); sdl->position->x += 10; } if (event.key.keysym.sym == SDLK_LEFT) { printf("LEFT\n"); sdl->position->x -= 10; } if (event.key.keysym.sym == SDLK_UP) { printf("UP\n"); sdl->position->y -= 10; } if (event.key.keysym.sym == SDLK_DOWN) { printf("DOWN\n"); sdl->position->y += 10; } } creation_rect(sdl); } return (0); } int creation_rect(t_sdl *sdl) { sdl->rectangle = NULL; if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, 255, 32, 45)) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if ((sdl->rectangle = SDL_CreateRGBSurface(SDL_HWSURFACE, 220, 180, 32, 0, 0, 0, 0)) == NULL) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_FillRect(sdl->rectangle, NULL, SDL_MapRGB(sdl->screen->format, 255, 255, 45)) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_BlitSurface(sdl->rectangle, NULL, sdl->screen, sdl->position) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } if (SDL_Flip(sdl->screen) == -1) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return (1); } return (0); } 3. h.h #ifndef H_H_ #define H_H_ /** * STRUCTURE */ typedef struct s_sdl { SDL_Surface *screen; SDL_Surface *rectangle; SDL_Rect *position; } t_sdl; /** * PROTOTYPE */ /* main.c */ int init(t_sdl *sdl); /* manager_sdl.c */ int manager_event(t_sdl *sdl); int creation_rect(t_sdl *sdl); #endif /* H_H_ */ 4. Compiling, linking, running, playing $ gcc main.c manager_sdl.c -c ; gcc main.o manager_sdl.o -lSDL -o go ; ./go Well done, you made it.

March 22, 2012 · Mi-K

C - Library functions - Using srand() and rand()

Today a tiny tutorial to see how using srand() and rand() from the libc. We will generate a number in a range between 0 and 2. For that, we will use the functions: time() srand() rand() sleep() The time() function because we want a random number that changes every second. We will use it with the sleep() function. We put a seed, generated by the time() function), into the srand() one. Then we display this number with the rand() function. And as we want a number between 0 and 2, we use the modulo operator (%). ...

March 9, 2012 · Mi-K

C - Errors - Handling errors

In this tutorial we will how to easily manage errors in C programming language. That is we will try to open a file and if this file doesn’t exist we will trigger an error. If the file exists, we will tell its name with a message to say that the file really exist. We have to notice that for handling errors correctly, we have to use the fprintf() function to indeed display error on the error output. For the standard output, we will use the classical printf() function. ...

March 8, 2012 · Mi-K

C - Errors / Warnings - After compiling

Errors and warnings generally encountered by a coder during his hard life of coder will be added here, in this special section. I will not make difference between errors and warnings, because I always compile with CFLAGS such as -Wall -Werror -Wextra and thus warnings are treated as errors. These errors will be those appearing after compiling a program. So we assume that we will just try to compile and of course it will fail. ...

January 8, 2012 · Mi-K

C - Recursion - Classical recursion of a factorial

A classical example of recursions is of course the factorial one. If you want to know the result of a factorial, do not hesitate to use a recursive function. Let’s see it: #include <stdio.h> int doIt(int nb) { if (nb <= 1) return 1; printf("nb = %d\n", nb); return nb * doIt(nb - 1); } int main() { int first = 5; printf("Last = %d\n", doIt(first)); return 0; } The render: nb = 5 nb = 4 nb = 3 nb = 2 Final result = 120

January 8, 2012 · Mi-K

C - Recursion - Finding a number until a variable reaches another one

The code below display the nb variable until it reaches the number ten. But the recursion will display the phrase in the printf(), of the main(), at the end of the while inside the r() function. So the code inside the r() function will be executed before the printf() of the main(). Let’s see this easy example: #include <stdio.h> int r(int nb) { while (nb < 10) { printf("nb = %d\n", nb); return r(++nb); } return 10; } int main() { int first = 1; printf("Last = %d\n", r(first)); return 0; } And the result: ...

January 8, 2012 · Mi-K

C - SDL (Simple Directmedia Layer) - Errors

Here a list of errors from the SDL library. These errors can be found from any circumstance, such as before compilation, after or during the linkage. Error 1 Description Resource Path Location Type undefined reference to `SDL_main’ line 315, external location: \Users\hercules\trunk\SDL-1.2\src\main\win32\SDL_win32_main.c C/C++ Problem To resolve this error on Windows, you have to pass the classic arguments int argc and *char argv[] to your main() function, just like this: int main(int argc, char *argv[]) { return 0; }

November 25, 2011 · Mi-K