Commit 3db2f154 authored by lochambeno's avatar lochambeno
Browse files

Le vaisseau bouge et s'incline selon la direction donnée

parent 2913c98a
......@@ -2,15 +2,21 @@
#include <SDL2/SDL_image.h>
#include "vaisseau.h"
//git checkout -t origin/...
int main(){
SDL_Window * window = NULL;
SDL_Renderer * renderer = NULL;
SDL_Texture * text_vaisseau = NULL;
SDL_Texture * text_vaisseau = NULL,
* text_vaisseauD = NULL,
* text_vaisseauG = NULL;
SDL_Rect vaisseau;
SDL_Rect vaisseau,
fond;
int fin_programme = 0;
int fin_programme = 0,
texture = 0;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Error : SDL initialisation - %s\n", SDL_GetError());
......@@ -18,7 +24,7 @@ int main(){
}
window = SDL_CreateWindow(
"Test_Texture",
"Jeu",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1080,
......@@ -48,36 +54,93 @@ int main(){
exit(EXIT_FAILURE);
}
text_vaisseauG = IMG_LoadTexture(renderer, "./images/redfighter1.png");
if (text_vaisseau == NULL){
SDL_DestroyTexture(text_vaisseau);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
SDL_Log("Error : window initialisation - %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
while (!fin_programme){
SDL_Event event;
while(!fin_programme && SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
fin_programme = 1;
break;
text_vaisseauD = IMG_LoadTexture(renderer, "./images/redfighter9.png");
if (text_vaisseau == NULL){
SDL_DestroyTexture(text_vaisseauG);
SDL_DestroyTexture(text_vaisseau);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
SDL_Log("Error : window initialisation - %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
//fond pour tester le vaisseau
fond.x = 0;
fond.y = 0;
fond.h = 720;
fond.w = 1080;
default:
break;
}
break;
vaisseau = init_vaisseau(window, text_vaisseau);
case SDL_MOUSEBUTTONDOWN:
while (!fin_programme){
SDL_Event event;
while(!fin_programme && SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
fin_programme = 1;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_RIGHT:
bouger_vaisseau_D(&vaisseau, 1080);
texture = 1;
break;
case SDLK_LEFT:
bouger_vaisseau_G(&vaisseau);
texture = 2;
break;
default:
break;
break;
}
}
afficher_vaisseau(vaisseau, text_vaisseau, renderer);
SDL_RenderPresent(renderer);
SDL_Delay(50);
}
break;
case SDL_MOUSEBUTTONDOWN:
default:
texture = 0;
break;
}
}
//fond blanc
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &fond);
if(texture == 1){
afficher_vaisseau(vaisseau, text_vaisseauD, renderer);
}
else
if(texture == 2){
afficher_vaisseau(vaisseau, text_vaisseauG, renderer);
}
else {
afficher_vaisseau(vaisseau, text_vaisseau, renderer);
}
/*SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &vaisseau);*/
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(text_vaisseauD);
SDL_DestroyTexture(text_vaisseauG);
SDL_DestroyTexture(text_vaisseau);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
}
\ No newline at end of file
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define VITESSE 3
#define VITESSE 20
//(x+max)%max+1
//(x+max)%max+1
SDL_Rect init_vaisseau(SDL_Window * window, SDL_Texture * text_vaisseau){
SDL_DisplayMode screen;
SDL_Rect vaisseau,
text_vaisseau_size;
SDL_Rect vaisseau;
SDL_GetCurrentDisplayMode(0, &screen);
int w_window, h_window, w_vaisseau, h_vaisseau;
SDL_GetWindowSize(window, &w_window, &h_window);
SDL_QueryTexture(text_vaisseau, NULL, NULL, &w_vaisseau, &h_vaisseau);
vaisseau.x = (w_window - w_vaisseau/4)/2;
vaisseau.y = h_window - h_vaisseau/4;
vaisseau.h = h_vaisseau/4;
vaisseau.w = w_vaisseau/4;
return vaisseau;
}
//bouge le vaisseau selon la touche pressee
void bouger_vaisseau_G(SDL_Rect * vaisseau){
if((*vaisseau).x - VITESSE <= 0)
(*vaisseau).x = 0;
if(vaisseau->x - VITESSE <= 0)
vaisseau->x = 0;
else
(*vaisseau).x -= VITESSE;
vaisseau->x -= VITESSE;
}
void bouger_vaisseau_D(SDL_Rect * vaisseau, int largeur_fenetre){
if((*vaisseau).x + VITESSE <= largeur_fenetre)
(*vaisseau).x = largeur_fenetre-1;
else
(*vaisseau).x += VITESSE;
if(vaisseau->x + VITESSE + vaisseau->w >= largeur_fenetre){
vaisseau->x = largeur_fenetre - vaisseau->w;
}
else{
vaisseau->x += VITESSE;
}
}
//actualise l'affichage du vaisseau
//actualise l'affichage du vaisseau
void afficher_vaisseau(SDL_Rect vaisseau, SDL_Texture * text_vaisseau, SDL_Renderer * renderer){
SDL_Rect source;
SDL_Rect source = {0};
SDL_QueryTexture(text_vaisseau, NULL, NULL, &source.w, &source.h);
SDL_RenderCopy(renderer, text_vaisseau, &source, &vaisseau);
......
#ifndef ZZ1_PROJET_CD_VAISSEAU_H
#define ZZ1_PROJET_CD_VAISSEAU_H
SDL_Rect init_vaisseau(SDL_Window);
SDL_Rect init_vaisseau(SDL_Window *, SDL_Texture *);
void bouger_vaisseau_G(SDL_Rect * );
void bouger_vaisseau_D(SDL_Rect * , int );
void afficher_vaisseau(SDL_Rect, SDL_Texture *, SDL_Renderer *);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment