Partage
  • Partager sur Facebook
  • Partager sur Twitter

SDL rapidité

Simple question à propos du refresh de l'écran

19 juin 2017 à 15:36:06

Bonjours à vous j'ai juste une petite questions concernant la SDL ( et la progr en générale )

Lorsque seulement une petite partie de l'écran et modifié. Est ce que c'est plus optimal de :

--Effacer tout l'écran et le "repeindre" avec les nouvelles donné avant de flip; ( ce qui est carrément plus simple )

ou :

--Rajouter des lignes de codes pour qu'il n'efface que les zone désirées et les repeignent avant de flip ? ( ce qui serait peut être ??? plus opti )

Si vous avez la réponse, elle serait la bienvenue :)

-
Edité par Golgoo 19 juin 2017 à 15:37:04

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 16:00:39

Salut,

Theoriquement, ne repeindre que la zone modifiée est plus rapide.
En pratique, tout depend de ce que ton appli fait.

PS: En général, avant de te poser les questions d'optimisation, occupe toi de faire quelque chose de fonctionnel.

Premature optimization is the root of all evil -- DonaldKnuth

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 16:04:17

Salut,

Disons que nos machines sont assez puissantes pour tout repeindre dans tous les cas.

Ne repeindre qu'une zone peut être rentable que si tu as très peu d'objets à déplacer, mais bon...

Mais si tu as pas mal, autant tout repeindre. 

Et si jamais tu un scrolling, la il n'y a pas à hésiter.


Dans les faits : ne t'embête pas et repeint tout !

  • Partager sur Facebook
  • Partager sur Twitter

Recueil de code C et C++  http://fvirtman.free.fr/recueil/index.html

19 juin 2017 à 16:22:55

Oui je suis partie pour tout repeindre les fonctions d'affichage sont bien plus simple comme ça.

C'est pour un tétris donc pas de prise de tête

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 16:37:16

Salut,

ça n'a rien à voir, mais il est relativement déconseillé d'utiliser une lib C telle que la SDL telle quelle en C++. Il conviendrait idéalement de tout encapsuler dans un wrapper RAII-conform afin de ne pas avoir à t'embêter à te taper l'allocation / libération de tes ressources.

En gros l'idée est de transformer du code tel que :

// Ces fonctions n'existent pas en vrai,
// c'est juste à titre explicatif 

// Allocation
SDL_Type *ptr = SDL_Alloc(/* Params */);
if (!ptr) {
  // Erreur
}
// Utilisation
const auto return_value = SDL_DoSomethingWith(ptr);
if (return_value != 0) {
  // Erreur
}
// Libération
SDL_Free(ptr);

En :

SDL_Type val{};   // Au pire ça lève une exception
val.DoSomething(); // Pareil
// A la fin du scope, val est détruit

Plus simple, non ?

Alors certes, ça demande un peu de boulot à la fin mais le résultat en vaut la chandelle. Si tu veux wrapper ça de manière simpliste, tu peux partir sur std::unique_ptr avec un truc du genre :

template<class T>
using Wrapper = std::unique_ptr<T, void(*)(T*)>;

Et à l'usage :

// Exemple pour SDL_Window
Wrapper<SDL_Window> window{SDL_CreateWindow(/* ... */), SDL_DestroyWindow};
// Exemple pour SDL_Surface
Wrapper<SDL_Surface> surface{SDL_LoadBMP(/* ... */), SDL_FreeSurface};

-
Edité par Emrak 19 juin 2017 à 16:38:07

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 17:44:23

Emrack c'est super simpa de vouloir m'apprendre des trucs, mais je n'ai clairement pas le niveau pour comprendre la moindre suite de phrase que tu dit :/ , la programmations avec les objets je croix qu'on commence ça en L2, je vais peut être attendre quelques cours avant de m'y penché d'avantage.

Sinon refresh tout le screen fait un léger flash sur l'écran, pas trop dérangeant quand le bloc bouge mais sur ce qui reste immobile c'est moche, je vote pour refresh le nécessaire perso :)

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 17:54:15

C'est justement parce que tu n'as pas le niveau (sans vouloir t'offenser ;) ) que je te propose de te débarrasser de la gestion manuelle des pointeurs ;)

Car c'est à mon sens le truc le plus lourd que tu auras à faire si tu veux utiliser (correctement) la SDL en C++.

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 18:05:16

Donc tu te lance dans un projet complexe avant de savoir programmer ? Pas top :/ Ou bien c'est du C, et tu es venu sur le mauvais forum (et encore, tu devrais comprendre les problèmes d'allocation/libération de ressource si c'est le cas).

Tu ne devrais pas obtenir de flash, je suis désolé de te dire que tu t'y es mal pris.

Tu ne gagneras rien à vouloir rafraichir que l'endroit concerné, les puces sont optimisées pour faire les rafraichissements de tout l'affichage, avec un double-buffering pour lisser tout ça, en voulant gérer ça toi-même tu risque seulement de faire des bêtises qui vont foirer l'affichage.

PS : Tu es bien sur un PC ? Sur de l'embarqué les règles peuvent peut-être être différentes ... mais je doute que tu soit dans ce cas.

  • Partager sur Facebook
  • Partager sur Twitter
Dream on, Dream on, Dream until your dream comes true
19 juin 2017 à 19:04:53

Bonsoir,

Je me pose la question si dans le cas ci-dessous, ne faut-il pas vérifier si le pointeur retourne NULL ?

Car, les fonctions de la SDL en langage C ne peuvent pas lever d'exception. Et unique_ptr ne leve pas non plus d'exception si le pointeur vaut NULL.


Emrak a écrit:


template<class T>
using Wrapper = std::unique_ptr<T, void(*)(T*)>;

Et à l'usage :

// Exemple pour SDL_Window
Wrapper<SDL_Window> window{SDL_CreateWindow(/* ... */), SDL_DestroyWindow};
// Exemple pour SDL_Surface
Wrapper<SDL_Surface> surface{SDL_LoadBMP(/* ... */), SDL_FreeSurface};

-
Edité par Emrak il y a environ 1 heure



  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 19:24:55

Oui sur PC et c++ mais je l'utilise a peu près comme du C.

 En effet je n'ai jamais entendu parler de pointeur d'objet, et là il s'agit d'un tetris qui n'utilise que quelques notions de plus que le sokoban, j'ai pas le niveau pour régler le petit flash, mais je peux commencer à voir pour éviter un maximum de refresh sur la surface fixe, c'est le seul truc qui dérange à l'oeuil nu.

Je vous file le code dès que c'est fini pour les plus curieux ;)

Le problème des gros flashs venait des flip, je pensais qu'il fallait absolument le faire avant de libérer la surface ( c a d chaque appel de fonction d'affichage ), mais non donc flash régler

-
Edité par Golgoo 19 juin 2017 à 19:46:35

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 19:33:50

GautierMaurice a écrit:

Oui sur PC et c++ mais je l'utilise a peu près comme du C.

Donc ton code est plein de bugs parce que C++ n'est pas C.

  • Partager sur Facebook
  • Partager sur Twitter

Posez vos questions ou discutez informatique, sur le Discord NaN | Tuto : Preuve de programmes C

19 juin 2017 à 19:34:34

cauld a écrit:

Bonsoir,

Je me pose la question si dans le cas ci-dessous, ne faut-il pas vérifier si le pointeur retourne NULL ?

Car, les fonctions de la SDL en langage C ne peuvent pas lever d'exception. Et unique_ptr ne leve pas non plus d'exception si le pointeur vaut NULL.

Si, complètement ;)

Pour ça que je parlais de wrapper simpliste ;)

En réalité faudrait une classe qui stocke un unique_ptr et vérifier dans le constructeur que le pointeur est non-nul.

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 21:02:45

GautierMaurice a écrit:

Le problème des gros flashs venait des flip, je pensais qu'il fallait absolument le faire avant de libérer la surface ( c a d chaque appel de fonction d'affichage ), mais non donc flash régler


Tu dois mal gérer les blit et les flip : on fait tous les blit, puis un seul flip.

Montre nous un bout de code pour voir :)

  • Partager sur Facebook
  • Partager sur Twitter

Recueil de code C et C++  http://fvirtman.free.fr/recueil/index.html

19 juin 2017 à 21:32:32

exact fvirtman, le problème a été réglé :),

pour l'instant ça avance ( et sans bug ), juste un peu embêtant de taper toute les rotation une a une, il doit y avoir une solution plus simple , je vous met le code entier bientôt et vous pourrez test par vous même.

  • Partager sur Facebook
  • Partager sur Twitter
19 juin 2017 à 21:41:01

GautierMaurice a écrit:

( et sans bug visible),

Fixed, très sérieusement du C dans C++, ce n'est très généralement pas bon.

  • Partager sur Facebook
  • Partager sur Twitter

Posez vos questions ou discutez informatique, sur le Discord NaN | Tuto : Preuve de programmes C

19 juin 2017 à 22:09:22

j'en doute pas ksass peuk on m'a déjà fait la même remarque ^^

Sinon si vous avez envie d ejouez a trétris voici le code ( la fonction bloc roation j'ai pas trouvé plus court et plus simple :/ )

si tu peux m'indiqué ce que j'aurai pus changer sous format cpp ça serait cool :)

J'aurai pu rendre certainnes fonctions un peu plus ergonomique, mais j'ai eu un peu la flemme

'header.h':

#ifndef __HEADER_H__
#define __HEADER_H__


#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>

//----------------------------------------

#define NB_PIXEL_HAUTEUR 850
#define NB_PIXEL_LARGEUR 578
#define EVENT_DELAY 45
#define BLOCK_DELAY 1000

#define SIZE_OF_BLOCK 4
#define NB_PIXEL_BLOCK 34
#define NB_BLOCK_HAUTEUR 25
#define NB_BLOCK_LARGEUR 17

//------------------------------------------

enum{EMPTY,FILL};
enum{SQUARE,BAR,SNAKE1,SNAKE2,FOOT1,FOOT2,PIT,NB_BLOCKS};
//enum{RED,BLUE,GREEN,NB_COLOR}
enum{DOWN,LEFT,RIGHT,NB_MOOVE};

//------------------------------------------

typedef struct Point
{
    int hauteur,largeur;
}Point;

typedef struct InfoBlock
{
    int index_block;
    int rotation_num;
}InfoBlock;

typedef struct Board
{
    bool matrice[NB_BLOCK_LARGEUR][NB_BLOCK_HAUTEUR];
    Point *block_focused;
    InfoBlock infoblock;
}Board;

//--------------------------------------------

void Assert(int test,int exit_num);
Board Board_Creation(void);
Point *Block_Creation(Board *b);
Point Point_Create(int hauteur,int largeur);
//---------------------------------------
bool Is_BlockCanAndMoove(Board *b,int moove_type);
bool Is_BlockNoCollistion(Board *b,Point bloc[SIZE_OF_BLOCK]);
void Block_MooveDown(Board *b);
void Block_Rotation(Board *b);

//----------------------------------------------
void Game(Board *board,SDL_Surface *screen);
int Block_EventOn(Board *board,SDL_Surface *screen);


//-------------------------------------------------

void Screen_Init(SDL_Surface *screen);
void Game_Print(Board *board,SDL_Surface *screen);
void Block_PrintOnScreen(Board *board,SDL_Surface *screen,Point position);
void Board_PrintFixed(Board *board,SDL_Surface *screen);
void Board_Destruction(Board *board);

bool Board_LigneDone(Board *b,int ligne_num);
void Board_DestructLigne(Board *b,int ligne_num);



#endif // header

main.cpp :

#include "header.h"

int main ( int argc, char** argv )
{
    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(NB_PIXEL_LARGEUR, NB_PIXEL_HAUTEUR, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
    Screen_Init(screen);
    SDL_Flip(screen);
    Board board=Board_Creation();

    Game(&board,screen);

    SDL_FreeSurface(screen);

    printf("Exited cleanly\n");
    return 0;
}

jeu.cpp ( je ne m'attendais pas à aussi court si j'avais su je n'aurai pas créer une nouvelle page )

void Game(Board *board,SDL_Surface *screen)
{
    bool done=false;
    while(!done)
    {
        board->block_focused=Block_Creation(board);
        Game_Print(board,screen);
        done=Block_EventOn(board,screen);
    }
}

graphique.cpp :

#include "header.h"

void Screen_Init(SDL_Surface *screen)
{
    SDL_Surface *ligne_horizontale=SDL_CreateRGBSurface(SDL_HWSURFACE,NB_PIXEL_LARGEUR, 1, 16, 0, 0, 0, 0);
    SDL_Surface *ligne_verticale=SDL_CreateRGBSurface(SDL_HWSURFACE, 1, NB_PIXEL_HAUTEUR, 16, 0, 0, 0, 0);


    SDL_FillRect(ligne_horizontale,NULL,SDL_MapRGB(screen->format,255,255,255));
    SDL_FillRect(ligne_verticale,NULL,SDL_MapRGB(screen->format,255,255,255));



    SDL_Rect position;
    position.x=0;
    position.y=0;

    for(position.x=0;position.x<NB_PIXEL_LARGEUR;position.x+=NB_PIXEL_BLOCK)
        SDL_BlitSurface(ligne_verticale,NULL,screen,&position);
    position.x=0;
    for(position.y=0;position.y<NB_PIXEL_HAUTEUR;position.y+=NB_PIXEL_BLOCK)
        SDL_BlitSurface(ligne_horizontale,NULL,screen,&position);
    //SDL_Flip(screen);
    SDL_FreeSurface(ligne_horizontale);
    SDL_FreeSurface(ligne_verticale);
}

void Block_PrintOnScreen(Board *board,SDL_Surface *screen,Point position)
{
    SDL_Surface *block_case=SDL_CreateRGBSurface(SDL_HWSURFACE,NB_PIXEL_BLOCK,NB_PIXEL_BLOCK,16,0,0,0,0);
    SDL_FillRect(block_case,NULL,SDL_MapRGB(screen->format,255,255,255));

    SDL_Rect position_pixel;
    position_pixel.y=position.hauteur*NB_PIXEL_BLOCK;
    position_pixel.x=position.largeur*NB_PIXEL_BLOCK;

    SDL_BlitSurface(block_case,NULL,screen,&position_pixel);

    //SDL_Flip(screen);
    SDL_FreeSurface(block_case);
}

void Board_PrintFixed(Board *board,SDL_Surface *screen)
{
    Point pos;

    for(int i=0;i<NB_BLOCK_LARGEUR;i++)
        for(int j=0;j<NB_BLOCK_HAUTEUR;j++)
            if(board->matrice[i][j]==FILL)
            {
                pos.largeur=i;pos.hauteur=j;
                Block_PrintOnScreen(board,screen,pos);
            }
    //SDL_Flip(screen);

}


void Game_Print(Board *board,SDL_Surface *screen)
{
    SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
    Screen_Init(screen);
    for(int i=0;i<SIZE_OF_BLOCK;i++)
        Block_PrintOnScreen(board,screen,board->block_focused[i]);
    Board_PrintFixed(board,screen);
    SDL_Flip(screen);
}

event.cpp

#include "header.h"

int Block_EventOn(Board *board,SDL_Surface *screen)
{
    bool continu=true;

    int temp_actuel=0;
    int check_point=SDL_GetTicks();
    int block_delay=1000;

    SDL_Event event;

    while(continu)
    {
        SDL_PollEvent(&event);
        switch(event.type)
        {
        case SDL_QUIT:
            return true;
            break;
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
            case SDLK_DOWN:
                continu=Is_BlockCanAndMoove(board,DOWN);
                Game_Print(board,screen);
                break;
            case SDLK_RIGHT:
                continu=Is_BlockCanAndMoove(board,RIGHT);
                Game_Print(board,screen);
                break;
            case SDLK_LEFT:
                continu=Is_BlockCanAndMoove(board,LEFT);
                Game_Print(board,screen);
                break;
            case SDLK_UP:
                Block_Rotation(board);
                Game_Print(board,screen);
                break;
            case SDLK_ESCAPE:
                return true;
                break;
            default :
                 ;
            }
            break;

        }

        temp_actuel=SDL_GetTicks();
        if(temp_actuel-check_point>block_delay)
        {
            continu=Is_BlockCanAndMoove(board,DOWN);
            Game_Print(board,screen);
            check_point=temp_actuel;
        }
        else
            SDL_Delay(EVENT_DELAY);
    }

    return false;
}

calcul.cpp

#include "header.h"

bool Is_BlockNoCollistion(Board *b,Point bloc[SIZE_OF_BLOCK])
{
    for(int i=0;i<SIZE_OF_BLOCK;i++)
        if(!(b->matrice[bloc[i].largeur][bloc[i].hauteur]==EMPTY && bloc[i].hauteur <NB_BLOCK_HAUTEUR && bloc[i].largeur<NB_BLOCK_LARGEUR && bloc[i].largeur>=0))
            return false;
    return true;
}

bool Is_BlockCanAndMoove(Board *b,int moove_type)
{
    Point static block_test[SIZE_OF_BLOCK];
    for(int i=0;i<SIZE_OF_BLOCK;i++)
    {
        block_test[i]=b->block_focused[i];
    }
    switch(moove_type)
    {
    case DOWN:
        for(int i=0;i<SIZE_OF_BLOCK;i++)
            block_test[i].hauteur++;
        break;
    case RIGHT:
        for(int i=0;i<SIZE_OF_BLOCK;i++)
            block_test[i].largeur++;
        break;
    case LEFT:
        for(int i=0;i<SIZE_OF_BLOCK;i++)
            block_test[i].largeur--;
        break;
    }
    if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
        }
    else
    {
        if(moove_type==DOWN)
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->matrice[b->block_focused[i].largeur][b->block_focused[i].hauteur]=FILL;
            Board_Destruction(b);
            return false;
        }
    }
    return true;
}

bool Board_LigneDone(Board *b,int ligne_num)
{
    for(int j=0;j<NB_BLOCK_LARGEUR;j++)
        if(b->matrice[j][ligne_num]==EMPTY)
            return false;
    return true;
}

void Board_DestructLigne(Board *b,int ligne_num)
{
    for(int j=0;j<NB_BLOCK_LARGEUR;j++)
        b->matrice[j][ligne_num]=EMPTY;
    for(int i=ligne_num;i>0;i--)
        for(int j=0;j<NB_BLOCK_LARGEUR;j++)
            b->matrice[j][i]=b->matrice[j][i-1];
}

void Board_Destruction(Board *board)
{
    for(int i=NB_BLOCK_HAUTEUR-1;i>0;i--)
        if(Board_LigneDone(board,i))
            Board_DestructLigne(board,i);
}

void Block_Rotation(Board *b)
{
   Point static block_test[SIZE_OF_BLOCK];
   for(int i=0;i<SIZE_OF_BLOCK;i++)
        block_test[i]=b->block_focused[i];
   switch(b->infoblock.index_block)
   {
   case SQUARE:
         ;
        break;
   case BAR:
        switch(b->infoblock.rotation_num)
        {
        case 0:
            block_test[0].hauteur--;
            block_test[0].largeur--;
            block_test[2].hauteur++;
            block_test[2].largeur++;
            block_test[3].hauteur+=2;
            block_test[3].largeur+=2;
            break;
        case 1:
            block_test[0].hauteur++;
            block_test[0].largeur++;
            block_test[2].hauteur--;
            block_test[2].largeur--;
            block_test[3].hauteur-=2;
            block_test[3].largeur-=2;
            break;
        }
        if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%2;
        }
        break;
    case SNAKE1:
        switch(b->infoblock.rotation_num)
        {
        case 0:
            block_test[1].hauteur++;
            block_test[1].largeur--;
            block_test[2].largeur+=2;
            block_test[3].hauteur++;
            block_test[3].largeur++;
            break;
        case 1:
            block_test[1].largeur++;
            block_test[1].hauteur--;
            block_test[2].largeur-=2;
            block_test[3].largeur--;
            block_test[3].hauteur--;
            break;
        }
        if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%2;
        }
    break;
        case SNAKE2:
        switch(b->infoblock.rotation_num)
        {
        case 0:
            block_test[0].hauteur++;
            block_test[0].largeur++;
            block_test[2].hauteur++;
            block_test[2].largeur--;
            block_test[3].largeur-=2;
            break;
        case 1:
            block_test[0].hauteur--;
            block_test[0].largeur--;
            block_test[2].hauteur--;
            block_test[2].largeur++;
            block_test[3].largeur+=2;
            break;
        }
        if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%2;
        }
    break;
        case FOOT1:
        switch(b->infoblock.rotation_num)
        {
            case 0:
                block_test[0].largeur+=2;
                block_test[1].hauteur--;
                block_test[1].largeur++;
                block_test[3].hauteur++;
                block_test[3].largeur--;
                break;
            case 1:
                block_test[0].hauteur-=2;
                block_test[1].hauteur--;
                block_test[1].largeur--;
                block_test[3].hauteur++;
                block_test[3].largeur++;
                break;
            case 2:
                block_test[0].largeur-=2;
                block_test[1].largeur--;
                block_test[1].hauteur++;
                block_test[3].largeur++;
                block_test[3].hauteur--;
                break;
            case 3:
                block_test[0].hauteur+=2;
                block_test[1].largeur++;
                block_test[1].hauteur++;
                block_test[3].hauteur--;
                block_test[3].largeur--;
                break;
        }
        if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%4;
        }
    break;
        case FOOT2:
            switch(b->infoblock.rotation_num)
        {
            case 0:
                block_test[0].largeur++;
                block_test[0].hauteur--;
                block_test[1].hauteur-=2;
                block_test[3].hauteur++;
                block_test[3].largeur--;
                break;
            case 1:
                block_test[1].largeur-=2;
                block_test[0].hauteur--;
                block_test[0].largeur--;
                block_test[3].hauteur++;
                block_test[3].largeur++;
                break;
            case 2:
                block_test[1].hauteur+=2;
                block_test[0].largeur--;
                block_test[0].hauteur++;
                block_test[3].largeur++;
                block_test[3].hauteur--;
                break;
            case 3:
                block_test[1].largeur+=2;
                block_test[0].largeur++;
                block_test[0].hauteur++;
                block_test[3].hauteur--;
                block_test[3].largeur--;
                break;
        }
        if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%4;
        }
        break;
            case PIT:
                switch(b->infoblock.rotation_num)
                {
                case 0:
                    block_test[0].hauteur++;
                    block_test[0].largeur++;
                    block_test[2].hauteur--;
                    block_test[2].largeur--;
                    block_test[3].hauteur++;
                    block_test[3].largeur--;
                    break;
                case 1:
                    block_test[0].hauteur--;
                    block_test[0].largeur++;
                    block_test[2].hauteur++;
                    block_test[2].largeur--;
                    block_test[3].hauteur++;
                    block_test[3].largeur++;
                    break;
                case 2:
                    block_test[0].hauteur--;
                    block_test[0].largeur--;
                    block_test[2].hauteur++;
                    block_test[2].largeur++;
                    block_test[3].hauteur--;
                    block_test[3].largeur++;
                    break;
                case 3:
                    block_test[0].hauteur++;
                    block_test[0].largeur--;
                    block_test[2].hauteur--;
                    block_test[2].largeur++;
                    block_test[3].hauteur--;
                    block_test[3].largeur--;
                    break;
                }
            if(Is_BlockNoCollistion(b,block_test))
            {
                for(int i=0;i<SIZE_OF_BLOCK;i++)
                    b->block_focused[i]=block_test[i];
                b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%4;
            }
            break;
   }
    SDL_Delay(55);
}

et enfin : init.cpp

#include "header.h"

Board Board_Creation(void)
{
    Board b;
    b.block_focused=NULL;
    //b.next=true;
    for(int i=0;i<NB_BLOCK_LARGEUR;i++)
        for(int j=0;j<NB_BLOCK_HAUTEUR;j++)
            b.matrice[i][j]=EMPTY;
    return b;
}

void Assert(int test,int exit_num)
{
    if(!test)
        exit(exit_num);
}

Point Point_Create(int hauteur,int largeur)
{
    Point p;
    p.hauteur=hauteur;
    p.largeur=largeur;
    return p;
}

Point *Block_Creation(Board *b)
{
    Point static *block_focused=(Point*)malloc(SIZE_OF_BLOCK*sizeof(Point));
    Assert(block_focused!=NULL,1);
    srand(time(NULL));
    b->infoblock.rotation_num=0;

    switch(rand()%NB_BLOCKS)
    {
    case SQUARE:
        block_focused[0]=Point_Create(1,8);
        block_focused[1]=Point_Create(1,9);
        block_focused[2]=Point_Create(0,8);
        block_focused[3]=Point_Create(0,9);
        b->infoblock.index_block=SQUARE;
        return block_focused;
    case BAR:
        block_focused[0]=Point_Create(3,8);
        block_focused[1]=Point_Create(2,8);
        block_focused[2]=Point_Create(1,8);
        block_focused[3]=Point_Create(0,8);
        b->infoblock.index_block=BAR;
        return block_focused;
    case SNAKE1:
        block_focused[0]=Point_Create(1,8);
        block_focused[1]=Point_Create(1,9);
        block_focused[2]=Point_Create(0,7);
        block_focused[3]=Point_Create(0,8);
        b->infoblock.index_block=SNAKE1;
        return block_focused;
    case SNAKE2:
        block_focused[0]=Point_Create(1,8);
        block_focused[1]=Point_Create(1,9);
        block_focused[2]=Point_Create(0,9);
        block_focused[3]=Point_Create(0,10);
        b->infoblock.index_block=SNAKE2;
        return block_focused;
    case FOOT1:
        block_focused[0]=Point_Create(2,7);
        block_focused[1]=Point_Create(2,8);
        block_focused[2]=Point_Create(1,8);
        block_focused[3]=Point_Create(0,8);
        b->infoblock.index_block=FOOT1;
        return block_focused;
    case FOOT2:
        block_focused[0]=Point_Create(2,7);
        block_focused[1]=Point_Create(2,8);
        block_focused[2]=Point_Create(1,7);
        block_focused[3]=Point_Create(0,7);
        b->infoblock.index_block=FOOT2;
        return block_focused;
    case PIT:
        block_focused[0]=Point_Create(1,7);
        block_focused[1]=Point_Create(1,8);
        block_focused[2]=Point_Create(1,9);
        block_focused[3]=Point_Create(0,8);
        b->infoblock.index_block=PIT;
        return block_focused;

    default:
        Assert(false,2);
    }

}





-
Edité par Golgoo 19 juin 2017 à 22:23:49

  • Partager sur Facebook
  • Partager sur Twitter
20 juin 2017 à 8:55:35

Ton code est propre pour un "débutant", bravo.

Il n'y a pas une seule ligne de C++ dedans cela dit (et c'est tant mieux :)).

Tu es sûr qu'on vous a demandé de réaliser ça en C++ ?

Attention par contre srand(time(NULL)); à faire une seule fois dans ton programme. Bouge ça hors de Block_Creation.

Et tu effectues un chargement de ressources à chaque tour de boucle (pire, à chaque affichage d'un bloc) impliquant lecture disque, il faut à tout prix limiter ça. Charge ton image une bonne fois pour toute (avec gestion des erreurs) et utilises la autant de fois qu'il te faut, puis à la fin du programme décharge là.

-
Edité par Scorbutics 20 juin 2017 à 9:05:42

  • Partager sur Facebook
  • Partager sur Twitter
20 juin 2017 à 9:47:41

Bon ben on a plus qu'à faire déplacer le sujet sur le forum C.

GautierMaurice a écrit:

si tu peux m'indiqué ce que j'aurai pus changer sous format cpp ça serait cool :)

Presque tout à vrai dire. Je ne sais même pas par quel bout attaquer mais comme ça m'amuse je vais juste tenter une réécriture en imaginant que j'ai déjà un wrapper SDL (je suis pas fou non plus). En tout cas, même en C, ta fonction "block_rotation" est à bannir, elle devrait être 10 fois moins longue.

-
Edité par Ksass`Peuk 20 juin 2017 à 9:50:36

  • Partager sur Facebook
  • Partager sur Twitter

Posez vos questions ou discutez informatique, sur le Discord NaN | Tuto : Preuve de programmes C

20 juin 2017 à 9:56:00

#ThisIsNotCPlusPlus
  • Partager sur Facebook
  • Partager sur Twitter

Si vous ne trouvez plus rien, cherchez autre chose.

20 juin 2017 à 10:37:57

GautierMaurice a écrit:

si tu peux m'indiqué ce que j'aurai pus changer sous format cpp ça serait cool :)

En C++, le premier truc à faire est de penser en terme de classes ou structures, et de services qu'elles doivent rendre pour communiquer et interragir entre-elles.

  • Partager sur Facebook
  • Partager sur Twitter
20 juin 2017 à 14:59:09

Tu pourras envoyer ton code une fois finis ksass ?

Scorbutic, par chargé l'image tu entend dire sauvegarder le quadrillage et un bloc blanc une bonne fois pour toute dans la structure board par exemple, et n'utiliser que ça dans les fonctions "graphique", puis décharger à la fin du prog, ou autre chose ?

Ksass, pour la fonction bloc rotation, je ne vois pas trop quoi faire pour l'optimisé.

if(Is_BlockNoCollistion(b,block_test))
        {
            for(int i=0;i<SIZE_OF_BLOCK;i++)
                b->block_focused[i]=block_test[i];
            b->infoblock.rotation_num=(b->infoblock.rotation_num+1)%4;
        }

Ca je pourrais le mettre qu'une seule fois à la fin, mais certaines ressemblance comme ceci :

case 0:
            block_test[0].hauteur++;
            block_test[0].largeur++;
            block_test[2].hauteur++;
            block_test[2].largeur--;
            block_test[3].largeur-=2;
            break;
        case 1:
            block_test[0].hauteur--;
            block_test[0].largeur--;
            block_test[2].hauteur--;
            block_test[2].largeur++;
            block_test[3].largeur+=2;
            break;

pour les "fusionner".. la solution ne me tape pas dans l'oeuil, après peut être faire intervenir les cosinus, sur chaque case du bloc qui doit bouger, les faire tourner de PI/2 autour du bloc central, arrondir pour avoir la bonne position et la sauvegarder si pas de collision. Mais ça a l'air chaud vu comme ça:/

PS: comment on fait pour le déplacer en C?



  • Partager sur Facebook
  • Partager sur Twitter
20 juin 2017 à 15:17:28

Pour ta fonction de rotation, tu pourrais créer une fonction , puis par un peu de calculs savants faire une rotation qui pourrait coller à chacune de tes pièces.

Par exemple, en y pensant un peu, on se rend compte que chacune des pièces peut rentrer dans un bloc de 4*4 cases, du coup tu pourrais appliquer cette rotation au bloc de 4*4 cases...

Si je m'explique mal, mes excuses, je vois la représentation dans ma tête mais mes doigts n'arrivent pas à l'exprimer :p

  • Partager sur Facebook
  • Partager sur Twitter
20 juin 2017 à 20:43:54

Yes je vois ça plus tard, si je trouve je post la fonction thx
  • Partager sur Facebook
  • Partager sur Twitter