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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL_ttf.h>
#define LARGEUR_WINDOW 600
#define LONGUEUR_WINDOW 800
void ExitWithError(const char* message);
void DestroyAndExitWithError(const char* message, SDL_Renderer* renderer, SDL_Window *window);
void afficherTexture(const char* chemin, SDL_Renderer* renderer, SDL_Window* window);
int main(int argc, char** argv)
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
ExitWithError("SDL n'a pas pu se lancer.");
}
window = SDL_CreateWindow("Titre de la fenetre", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LONGUEUR_WINDOW, LARGEUR_WINDOW, 0);
if (window == NULL) {
ExitWithError("La fenetre n'a pas pu s'ouvrir.");
}
/*-----------------------------------------------------------*/
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL) {
ExitWithError("Création rendu échoué.");
}
if (TTF_Init() == -1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
TTF_Font* police = NULL;
police = TTF_OpenFont("../../../../Desktop/polices/Outwrite.ttf", 65);
SDL_Color couleur = { 255, 255, 255 };
afficherTexture("../../../../Desktop/ListeOfImg/imagetest.bmp", renderer, window);
afficherTexture("../../../../Desktop/ListeOfImg/super-mario-title.bmp", renderer, window);
SDL_RenderPresent(renderer);
SDL_bool program_launched = SDL_TRUE;
SDL_Texture* Message = NULL;
SDL_Surface* scoreSurface;
int score = 0;
while (program_launched)
{
SDL_Event event = { 0 };
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
program_launched = SDL_FALSE;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE) {
score++;
scoreSurface = TTF_RenderText_Solid(police, &score, couleur);
if (scoreSurface == NULL) {
TTF_CloseFont(police);
TTF_Quit();
DestroyAndExitWithError("TTF_RenderText_Solid", renderer, window);
}
Message = SDL_CreateTextureFromSurface(renderer, scoreSurface);
if (Message == NULL) {
TTF_CloseFont(police);
TTF_Quit();
DestroyAndExitWithError("SDL_CreateTextureFromSurface", renderer, window);
}
SDL_FreeSurface(scoreSurface);
SDL_Rect rectangleText = { 0, 0, 100, 100 };
if (SDL_RenderCopy(renderer, Message, NULL, &rectangleText) != 0) {
TTF_CloseFont(police);
TTF_Quit();
DestroyAndExitWithError("SDL_CreateTextureFromSurface", renderer, window);
}
SDL_RenderPresent(renderer);
}
continue;
default:
continue;
}
}
}
/*-----------------------------------------------------------*/ |
Partager