IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SDL Discussion :

Dessiner un pixel à la putpixel() mais dans une autre SDL_Surface que celle flipé


Sujet :

SDL

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Dessiner un pixel à la putpixel() mais dans une autre SDL_Surface que celle flipé
    Hello à tous.

    Bon voila, ça fait longtemps que j'ai pas fait de prog, et je m'y remets doucement, j'ai commencé un petit jeu pour m'initier à la SDL, j'ai commencé avec des images pour les sprites, puis je suis passé à l'OpenGl, bon heureusement j'étais pas trop avancé, j'ai décidé de me passer d'OpenGl et de faire des graphismes vectoriels en dessinant des pixels... (je m'initierai à l'OpenGl lors de mon prochain projet), du coup j'ai cherché une fonction pour dessiner un pixel, j'ai trouvé putpixel dans la doc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int bpp = surface->format->BytesPerPixel;
        // Here p is the address to the pixel we want to set
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
        switch(bpp) {
        case 1:
            *p = pixel;
            break;
     
        case 2:
            *(Uint16 *)p = pixel;
            break;
     
        case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            } else {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
     
        case 4:
            *(Uint32 *)p = pixel;
            break;
        }
    }
    seulement avec cette fonction je peux uniquement dessiner dans la SDL_Surface *screen qui est destiné à être flipé : SDL_Flip(screen)
    ça ne fonctionne pas avec une autre SDL_Surface *

    or je ne veux pas tout redessiner à l'écran à chaque fois of course, je veux stocker mes sprites...

    donc comment faire pour dessiner dans une SDL_Surface autre que celle destiné à être affiché à l'écran ?

    Je suppose que ça vient peut être de l'initialisation de la SDL_Surface, mais j'ai pas réussi à trouver sur le net de quelle manière je dois l'initialiser...

    Merci d'avoir lu, et si vous pouviez m'aider ça serait bien cool ^^

  2. #2
    Membre expérimenté
    Avatar de coyotte507
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    1 327
    Détails du profil
    Informations personnelles :
    Âge : 33
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 327
    Points : 1 452
    Points
    1 452
    Par défaut
    Salut,

    ça marche avec n'importe quelle surface, pourvu qu'elle soit initialisée.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <SDL/SDL.h>
    #include <iostream>
     
    using namespace std;
     
    SDL_Surface *affichage;
     
    void exit_error(const char *desc)
    {
    	cout << "Error -- " << desc << SDL_GetError() << endl;
    	exit(1);
    }
     
    void attendre_touche()
    {
    	SDL_Event event;
    	while(SDL_WaitEvent(&event))
    	{
    		switch(event.type)
    		{
    			case SDL_QUIT:
    				return;
    			case SDL_KEYDOWN:
    				return;
    			default:
    				break;
    		}
    	}
    }
     
    void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int bpp = surface->format->BytesPerPixel;
        // Here p is the address to the pixel we want to set
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
        switch(bpp) {
        case 1:
            *p = pixel;
            break;
     
        case 2:
            *(Uint16 *)p = pixel;
            break;
     
        case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            } else {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
     
        case 4:
            *(Uint32 *)p = pixel;
            break;
        }
    }
     
    int main(int argc, char **argv)
    {
    	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    		exit_error("SDL_Init: ");
    	}
     
    	atexit(SDL_Quit);
     
    	affichage = SDL_SetVideoMode(400, 400, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    	if (affichage == NULL) {
    		exit_error("SDL_SetVideoMode");
    	}
     
    	SDL_Surface *x = SDL_CreateRGBSurface(affichage->flags, 400, 400, affichage->format->BitsPerPixel, 0, 0, 0,0);
     
        if (SDL_MUSTLOCK(x)) SDL_LockSurface(x); //nécessaire sinon crash dans certain cas
        putpixel(x, 15,15 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 15,16 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 16,15 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 16,16 , SDL_MapRGB(x->format, 255,255,255));
        if (SDL_MUSTLOCK(x)) SDL_UnlockSurface(x);
     
    	SDL_BlitSurface(x, NULL, affichage, NULL);
     
    	SDL_Flip(affichage);
     
    	attendre_touche();
     
    	SDL_Quit();
     
    	return 0;
    }
    D'ailleurs des trucs intéressants sont faits dans les tutos d'Anomaly

  3. #3
    Membre émérite Avatar de SofEvans
    Homme Profil pro
    Développeur C
    Inscrit en
    Mars 2009
    Messages
    1 076
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur C

    Informations forums :
    Inscription : Mars 2009
    Messages : 1 076
    Points : 2 328
    Points
    2 328
    Par défaut
    Salut,

    Je ne vois pas pourquoi cela ne marcherai pas avec une autre surface.

    La fonction putpixel prend un pixel de tel couleur, le met a tel endroit de telle surface, point barre. Si la surface n'existe pas, un code d'erreur est retourner.

    La surface transmise par paramétré n'a rien a voir au fait qu'elle sera "flipé" ou non.
    Par contre, si tu ne flip pas la surface ou ne blit pas la surface qui a été modifier, tu ne verra rien.

  4. #4
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par coyotte507 Voir le message
    Salut,

    ça marche avec n'importe quelle surface, pourvu qu'elle soit initialisée.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <SDL/SDL.h>
    #include <iostream>
     
    using namespace std;
     
    SDL_Surface *affichage;
     
    void exit_error(const char *desc)
    {
    	cout << "Error -- " << desc << SDL_GetError() << endl;
    	exit(1);
    }
     
    void attendre_touche()
    {
    	SDL_Event event;
    	while(SDL_WaitEvent(&event))
    	{
    		switch(event.type)
    		{
    			case SDL_QUIT:
    				return;
    			case SDL_KEYDOWN:
    				return;
    			default:
    				break;
    		}
    	}
    }
     
    void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int bpp = surface->format->BytesPerPixel;
        // Here p is the address to the pixel we want to set
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
        switch(bpp) {
        case 1:
            *p = pixel;
            break;
     
        case 2:
            *(Uint16 *)p = pixel;
            break;
     
        case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            } else {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
     
        case 4:
            *(Uint32 *)p = pixel;
            break;
        }
    }
     
    int main(int argc, char **argv)
    {
    	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    		exit_error("SDL_Init: ");
    	}
     
    	atexit(SDL_Quit);
     
    	affichage = SDL_SetVideoMode(400, 400, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    	if (affichage == NULL) {
    		exit_error("SDL_SetVideoMode");
    	}
     
    	SDL_Surface *x = SDL_CreateRGBSurface(affichage->flags, 400, 400, affichage->format->BitsPerPixel, 0, 0, 0,0);
     
        if (SDL_MUSTLOCK(x)) SDL_LockSurface(x); //nécessaire sinon crash dans certain cas
        putpixel(x, 15,15 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 15,16 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 16,15 , SDL_MapRGB(x->format, 255,255,255));
        putpixel(x, 16,16 , SDL_MapRGB(x->format, 255,255,255));
        if (SDL_MUSTLOCK(x)) SDL_UnlockSurface(x);
     
    	SDL_BlitSurface(x, NULL, affichage, NULL);
     
    	SDL_Flip(affichage);
     
    	attendre_touche();
     
    	SDL_Quit();
     
    	return 0;
    }
    D'ailleurs des trucs intéressants sont faits dans les tutos d'Anomaly
    okay merci

    SDL_Surface *x = SDL_CreateRGBSurface(affichage->flags, 400, 400, affichage->format->BitsPerPixel, 0, 0, 0,0);

    ça me met sur la voie ça...

    edit: ils sont où ces tutos ? parce que j'ai regardé sur différents tutos ici et aileurs et j'ai du passer à côté..

  5. #5
    Membre expérimenté
    Avatar de coyotte507
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    1 327
    Détails du profil
    Informations personnelles :
    Âge : 33
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 327
    Points : 1 452
    Points
    1 452
    Par défaut
    Le site a changé, je suis obligé d'aller sur google pour trouver les tutos

    http://anomaly.developpez.com/tutoriel/sdl/

    et d'autres tutos:

    http://fearyourself.developpez.com/
    http://loka.developpez.com/tutoriel/sdl/

    il y a aussi la doc SDL:

    http://www.libsdl.org/cgi/docwiki.cgi

  6. #6
    Rédacteur

    Avatar de loka
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2004
    Messages
    2 672
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Service public

    Informations forums :
    Inscription : Novembre 2004
    Messages : 2 672
    Points : 5 509
    Points
    5 509
    Par défaut
    Citation Envoyé par coyotte507 Voir le message
    Le site a changé, je suis obligé d'aller sur google pour trouver les tutos
    Tu peux toujours retrouver les tutoriels 2D/3D/jeux ici :
    http://jeux.developpez.com/tutoriels/

    et donc ceux sur SDL ici :
    http://jeux.developpez.com/tutoriels/?page=prog2d#sdl

    La page d'index 2D/3D/jeux est toujours http://jeux.developpez.com

  7. #7
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Thanks pour les liens, bon j'ai réussi à résoudre mon problème j'étais pas loin du tout en fait, ça me fait un peu râler de pas avoir trouver tout seul, par contre la page de la fonction SDL_CreateRGBSurface sur la doc sdl officielle est vide

  8. #8
    Membre expérimenté
    Avatar de coyotte507
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    1 327
    Détails du profil
    Informations personnelles :
    Âge : 33
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 327
    Points : 1 452
    Points
    1 452

Discussions similaires

  1. [Python 3.X] PyQt4: supprimer un item d'un QListWidget dans une autre fonction que celle où il a été créé
    Par nilslauwers dans le forum Bibliothèques tierces
    Réponses: 16
    Dernier message: 20/05/2015, 12h52
  2. Réponses: 1
    Dernier message: 16/11/2014, 17h46
  3. Réponses: 6
    Dernier message: 05/09/2013, 18h20
  4. Réponses: 5
    Dernier message: 27/11/2008, 17h34
  5. Ecrire dans une autre fenetre que celle de l'application
    Par aurelien59 dans le forum Windows
    Réponses: 3
    Dernier message: 15/05/2007, 11h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo