Bonjour,
Je cherche à réaliser une petite animation qui me permettrait de faire évoluer un rectangle sur l'axe vertical. J'ai "connecté" ce rectangle avec un appareil qui permet de collecter les données spatiales (sur l'axe vertical).
Je réalise cela avec la bibliothèque SDL.
J'obtiens bien le déplacement dudit rectangle mais les précédentes positions du rectangle restent affichées sur ma fenêtre.
Je suis débutant en C++ , auriez-vous une idée de ce qui se passe?
Merci!
mon code:
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
 
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include "ATC3DG.h"
#include "Sample.h"
#include <iostream>
#include <time.h>
 
using namespace std;
 
 
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
 
//Starts up SDL and creates window
bool init();
 
//Frees media and shuts down SDL
void close();
 
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
 
//The window renderer
SDL_Renderer* gRenderer = NULL;
 
bool init()
{
	//Initialization flag
	bool success = true;
 
	//Initialize SDL
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Set texture filtering to linear
		if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
		{
			printf( "Warning: Linear texture filtering not enabled!" );
		}
 
		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
		if( gWindow == NULL )
		{
			printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
			success = false;
		}
		else
		{
			//Create renderer for window
			gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
			if( gRenderer == NULL )
			{
				printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
				success = false;
			}
			else
			{
				//Initialize renderer color
				SDL_SetRenderDrawColor( gRenderer, 0, 0, 0, 1 );
			}
		}
	}
	return success;
}
 
void close()
{
	//Destroy window
	SDL_DestroyRenderer( gRenderer );
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;
	gRenderer = NULL;
 
	//Quit SDL subsystems
 
	SDL_Quit();
}
 
int main( int argc, char* args[] )
{
	CSystem			ATC3DG; // pointeur vers une structure
    CSensor			*pSensor; // pointeur vers une structure
    CXmtr			*pXmtr;//pointeur vers une structure
    int             i;
    short           id;
    int errorCode;
    clock_t			goal;
	clock_t			wait=10;
	int				records = 100;
	char			output[256];
	int				sensorID;
	int				numberBytes;
 
// Initialisation du Trakstar
    printf("InitializCXmtring ATC3DG system...\n");
	errorCode = InitializeBIRDSystem();
	if(errorCode!=BIRD_ERROR_SUCCESS) cout << "erreur lors de l'initialisation" <<endl;
 
 
	errorCode = GetBIRDSystemConfiguration(&ATC3DG.m_config);
	if(errorCode!=BIRD_ERROR_SUCCESS) cout << "erreur lors du Get Bird System config" <<endl;
    cout << "frequence de mesure " << ATC3DG.m_config.measurementRate<< "Hz" <<endl; // affiche fréquence de mesure du trakstar
 
    pSensor = new CSensor[ATC3DG.m_config.numberSensors];
	for(i=0;i<ATC3DG.m_config.numberSensors;i++)
	{
		errorCode = GetSensorConfiguration(i, &(pSensor+i)->m_config);
		if(errorCode!=BIRD_ERROR_SUCCESS) cout << "erreur lors du Get sensor config" <<endl;
	}
 
 
    pXmtr = new CXmtr[ATC3DG.m_config.numberTransmitters];
	for(i=0;i<ATC3DG.m_config.numberTransmitters;i++)
	{
		errorCode = GetTransmitterConfiguration(i, &(pXmtr+i)->m_config);
		if(errorCode!=BIRD_ERROR_SUCCESS) cout << "erreur lors de l'obtention de la configuration du transmetteur" <<endl;
	}
 
// Recherche du transmetteur présent et on le met en mode ON
	for(id=0;id<ATC3DG.m_config.numberTransmitters;id++)
	{
		if((pXmtr+id)->m_config.attached)
		{
			errorCode = SetSystemParameter(SELECT_TRANSMITTER, &id, sizeof(id));
			if(errorCode!=BIRD_ERROR_SUCCESS) cout << "erreur lors lors de la mise en route du transmetteur" <<endl;
			break;
		}
	}
 
// Collecte des données du trakstar en mode asynchrone, ce mode retourne la donnée enregistrée
// du dernier cycle d'enregistrement. Si cette fonction est appellée à une fréquence plus importante
// que la fréquence d'acquisition du trackstar alors on aura des données similaires.
 
 
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
			//Main loop flag
			bool quit = false;
 
			//Event handler
			SDL_Event e;
 
			//Clear screen
            SDL_SetRenderDrawColor( gRenderer, 0, 0, 0, 1 );
            SDL_RenderClear( gRenderer );
 
            DOUBLE_POSITION_ANGLES_RECORD record, *pRecord = &record;
 
			//While application is running
			while( !quit )
			{
 
				//User requests quit
				if( e.type == SDL_QUIT )
				{
					quit = true;
				}
 
                goal = wait + clock();
 
                // collect as many records as specified in the command line
                for(i=0;i<records;i++)
                {
                    // delay 10ms between collecting data
                    // wait till time delay expires
                    while(goal>clock());
                    // set up time delay for next loop
                    goal = wait + clock();
 
                    // scan the sensors and request a record
                    for(sensorID=0;sensorID<ATC3DG.m_config.numberSensors;sensorID++)
                    {
                        // sensor attached so get record
                        errorCode = GetAsynchronousRecord(sensorID, pRecord, sizeof(record));
                        if(errorCode!=BIRD_ERROR_SUCCESS) {cout << "erreur lors lors la transmission des données" <<endl;}
 
                        // Obtient le status de la dernière donnée enregistrée
                        // Rapporte seulement la données si tout est OK
                        unsigned int status = GetSensorStatus( sensorID);
 
                        if( status == VALID_STATUS)
                        {
                            //Render red filled quad
                            SDL_Rect fillRect = { SCREEN_WIDTH /2-SCREEN_WIDTH / 12, SCREEN_HEIGHT-record.y*5000, SCREEN_WIDTH / 6, SCREEN_HEIGHT / 25 };
                            SDL_SetRenderDrawColor( gRenderer, 255, 255, 255, 1 );
                            SDL_RenderFillRect( gRenderer, &fillRect );
                            //Update screen
                            SDL_RenderPresent( gRenderer );
                        }
                    }
 
                }
            }
        }
 
	//Free resources and close SDL
	close();
 
	return 0;
}