Tutorial 1
We create a very simple (non-)game, just showing a centered text ("Press space to exit")
and when pressing the space-key the program exits.
We show two ways of compiling with two Makefiles:
- Makefile:
the normal way, running the game will require an installed VgaGames3-library
- Makefile.localinstall:
while copying required files of the installed VgaGames3-library into the current path,
running the game will not require an installed VgaGames3-library
- Step 1
We open a window, using a program parameter for the scaling of the window, then close it again.#include <vgagames3.h> int main(int argc, char **argv) { void *wstruct; int i1, scale; /* read command parameters */ scale = VGAG3_WINSCALE_NOSCALE; opterr = opterr ? opterr : 1; while ((i1 = getopt(argc, argv, "+s:h")) != -1) { switch(i1) { case 's': if (strcmp(optarg, "no") == 0) { scale = VGAG3_WINSCALE_NOSCALE; } else if (strcmp(optarg, "best") == 0) { scale = VGAG3_WINSCALE_BESTSCALE; } else if (strcmp(optarg, "max") == 0) { scale = VGAG3_WINSCALE_MAXIMIZE; } else if (strcmp(optarg, "full") == 0) { scale = VGAG3_WINSCALE_FULLSCREEN; } break; case 'h': default: fprintf(stderr, "Usage: %s [<options>]\n", argv[0]); fprintf(stderr, "options:\n"); fprintf(stderr, " -s <scaling>: scale window according to <scaling>:\n"); fprintf(stderr, " - no: no scaling\n"); fprintf(stderr, " - best: best scaling\n"); fprintf(stderr, " - max: maximize window\n"); fprintf(stderr, " - full: fullscreen\n"); exit(1); } } /* open window */ wstruct = VG3_window_new(argv[0], VGAG3_VGAVERSION_LOW, scale); if (wstruct == NULL) { fprintf(stderr, "%s\n", VG3_error()); exit(1); } /* close window and exit */ VG3_window_free(wstruct); exit(0); }
- Step 2
We create a text, simulate drawing it onto the window to get the width and height of it and then draw it onto the windowint winw, winh; struct vg3_text stxt; struct vg3_rect rect; /* get the size of the window */ VG3_window_getsize(wstruct, &winw, &winh); /* create text with standard font */ VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, "Press space to exit"); /* simulate drawing text to get real size in width and height */ rect.x = 0; rect.w = winw; rect.y = 0; rect.h = winh; rect = VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 1); /* now we got in rect.w and rect.h the size of the text */ /* correct x/y-position to draw text horizontal and vertical centered */ rect.x = (winw - rect.w) / 2; rect.y = (winh - rect.h) / 2; /* draw text onto the window */ VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK); VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 0);
- Step 3
Now we create the game-loop.
As we drawn the text just onto the window and don't want to change the window contents anymore, it is not needed to clear and redraw it again, just to update the shown contents./* update window contents and wait for pressing space-key */ VG3_discard_input(wstruct); for (;;) { if (VG3_inputevent_update(wstruct) > 0) { break; } if (VG3_key_ispressed(wstruct, VGAG3_KEY_SPACE, VGAG3_IS_NEW_PRESSED)) { break; } VG3_window_update(wstruct, 0, 0); VG3_wait_time(50); } VG3_discard_input(wstruct);
- Putting together
Finally we got the following file (main.c):#include <vgagames3.h> int main(int argc, char **argv) { void *wstruct; int i1, scale; int winw, winh; struct vg3_text stxt; struct vg3_rect rect; /* read command parameters */ scale = VGAG3_WINSCALE_NOSCALE; opterr = opterr ? opterr : 1; while ((i1 = getopt(argc, argv, "+s:h")) != -1) { switch(i1) { case 's': if (strcmp(optarg, "no") == 0) { scale = VGAG3_WINSCALE_NOSCALE; } else if (strcmp(optarg, "best") == 0) { scale = VGAG3_WINSCALE_BESTSCALE; } else if (strcmp(optarg, "max") == 0) { scale = VGAG3_WINSCALE_MAXIMIZE; } else if (strcmp(optarg, "full") == 0) { scale = VGAG3_WINSCALE_FULLSCREEN; } break; case 'h': default: fprintf(stderr, "Usage: %s [<options>]\n", argv[0]); fprintf(stderr, "options:\n"); fprintf(stderr, " -s <scaling>: scale window according to <scaling>:\n"); fprintf(stderr, " - no: no scaling\n"); fprintf(stderr, " - best: best scaling\n"); fprintf(stderr, " - max: maximize window\n"); fprintf(stderr, " - full: fullscreen\n"); exit(1); } } /* open window */ wstruct = VG3_window_new(argv[0], VGAG3_VGAVERSION_LOW, scale); if (wstruct == NULL) { fprintf(stderr, "%s\n", VG3_error()); exit(1); } /* get the size of the window */ VG3_window_getsize(wstruct, &winw, &winh); /* create text with standard font */ VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, "Press space to exit"); /* simulate drawing text to get real size in width and height */ rect.x = 0; rect.w = winw; rect.y = 0; rect.h = winh; rect = VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 1); /* now we got in rect.w and rect.h the size of the text */ /* correct x/y-position to draw text horizontal and vertical centered */ rect.x = (winw - rect.w) / 2; rect.y = (winh - rect.h) / 2; /* draw text onto the window */ VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK); VG3_draw_text(wstruct, NULL, &rect, ' ', &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLUE, 0); /* update window contents and wait for pressing space-key */ VG3_discard_input(wstruct); for (;;) { if (VG3_inputevent_update(wstruct) > 0) { break; } if (VG3_key_ispressed(wstruct, VGAG3_KEY_SPACE, VGAG3_IS_NEW_PRESSED)) { break; } VG3_window_update(wstruct, 0, 0); VG3_wait_time(50); } VG3_discard_input(wstruct); /* close window and exit */ VG3_window_free(wstruct); exit(0); }
- Compiling
- Makefile
Compiling the game, where an installation of the VgaGames3-library is required:PREFIX = /usr/local CFLAGS = -W -Wall -O2 VGAG_CFLAGS = `vgagames3-config --cflags` VGAG_LIBS = `vgagames3-config --libspath` # +++ compile +++ all: game game: main.o $(CC) main.o $(VGAG_LIBS) -o game # +++ install +++ install: mkdir -p $(PREFIX)/bin cp game $(PREFIX)/bin/ && chmod 755 $(PREFIX)/bin/game # +++ uninstall +++ uninstall: rm -f $(PREFIX)/bin/game # +++ clean +++ clean: rm -f game main.o # +++ object files +++ main.o: main.c $(CC) $(CFLAGS) $(VGAG_CFLAGS) -c main.c
- Makefile.localinstall
Compiling the game with copying the required files of the installed VgaGames3-library into the current directory, for running the game without an installed VgaGames3-library.
With vgagames3-config --copylocal a directory vgag3 will be created and the required files will be copied into it.
Then the makefile creates a start-script, which sets two environment variables before calling the game.
Compile with: make -f Makefile.localinstall all installCFLAGS = -W -Wall -O2 VGAG_CFLAGS = `vgagames3-config --cflags` # Don't use --libspath VGAG_LIBS = `vgagames3-config --libs` # +++ compile +++ all: game game: main.o $(CC) main.o $(VGAG_LIBS) -o game # +++ install +++ install: @mkdir exec @(cd exec && vgagames3-config --copylocal >/dev/null) @cp game exec/ && chmod 755 exec/game @# create start script @(cd exec && { \ echo LD_LIBRARY_PATH=vgag3/lib VGAG3_PREFIX=vgag3 ./game \"$$\@\" >startgame.sh; \ chmod 755 startgame.sh; }; \ ) @echo; echo "Go to exec/ and type ./startgame.sh [
]" # +++ uninstall +++ uninstall: rm -rf exec # +++ clean +++ clean: rm -f game main.o # +++ object files +++ main.o: main.c $(CC) $(CFLAGS) $(VGAG_CFLAGS) -c main.c - Running
Running the game
<<Prev | Top | Next>> |