SDL2 Rendert 10x image classe ..Aber nicht hintereinander
- 
					
					
					
					
 Guten Tag. 
 Wow hat sich das hier verändert 
 Ich habe ein Problem und zwar geht es um folgendes ... Ich versuche 10x die gleiche Textur mithilfe einer Klasse zu render .. das Problem: rendere ich sie mit Zufalls Zahlen funktioniert alles Will ich jedoch nur mit "pos.x + 50" rendern in der for-loop, klappt das ganze nicht Was mache ich falsch ??? #include <SDL2/SDL.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <cstdlib> // this is for the classes #include <sstream> // and this for "arrayof" #define hWnd_Button_height 48 #define hWnd_Button_width 42 SDL_Surface* image = NULL; SDL_Texture* texture_image = NULL; SDL_Rect dstrect; //____ the class ____ const int MAX_image = 10; class image_class { public: image_class(); bool isActive; int image_pos_x; int image_pos_y; int image_size_x; int image_size_y; SDL_Renderer* renderer = NULL; SDL_Surface* surface = NULL; SDL_Texture* texture = NULL; void show( SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect ); private: }; image_class arrayofPanel[MAX_image]; image_class::image_class() { image_pos_x = 300; image_pos_y= 300; image_size_x = 96; image_size_y = 84; } // ____________ void image_class::show ( SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect ) { // we clone 10x the rect to int for (int i=0; i<MAX_image; i++) { if (arrayofPanel[i].isActive== true) { SDL_Rect dstrect2; dstrect2.x = arrayofPanel[i].image_pos_x; dstrect2.y = arrayofPanel[i].image_pos_y; dstrect2.w = arrayofPanel[i].image_size_x; dstrect2.h= arrayofPanel[i].image_size_y; //draw bitmap SDL_RenderCopy(renderer, texture_image, NULL, &dstrect2); } } } //_________MAIN ____________ int main( int argc, char *argv[] ) { SDL_Event event; SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("SDL2 image_Class ", 0, 0, 1024, 768, SDL_WINDOW_SHOWN); SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); //-------------- image = SDL_LoadBMP("hwnd_blank_up.bmp"); texture_image = SDL_CreateTextureFromSurface(renderer,image); //---------------- SDL_Surface *hWnd_down = SDL_LoadBMP("hwnd_blank_down.bmp"); SDL_Texture *texture_hWnd_down = SDL_CreateTextureFromSurface(renderer,hWnd_down); //-------------- // load the class + information image_class myClass; //------------------------------------------------- int quit = 1; int system_var = 2; while(quit == 1) // prgramm loop { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = 0; break; } } //--------------- // clone 10x X and Y.... 1 time!!! if (system_var == 2) { srand(time(NULL)); for (int i=0; i<MAX_image; i++) { arrayofPanel[i].image_pos_x = 300; arrayofPanel[i].image_pos_y = 300; } for (int i=0; i<MAX_image; i++) { arrayofPanel[i].image_pos_x += rand()%600+50; arrayofPanel[i].image_pos_y += rand()%600+50; // arrayofPanel[i].image_size_x; // arrayofPanel[i].image_size_y; // arrayofPanel[i].image_pos_x += 50; arrayofPanel[i].isActive= true; } system_var = 1; } //--------------- //clone 10x surfaces on 10 pos for (int i=0; i<MAX_image; i++) { if (arrayofPanel[i].isActive== true) { dstrect.x = arrayofPanel[i].image_pos_x; dstrect.y = arrayofPanel[i].image_pos_y; // dstrect.h = arrayofPanel[i].image_size_x; // dstrect.w = arrayofPanel[i].image_size_y; SDL_RenderCopy(renderer, texture_image, NULL, &dstrect); } } myClass.show(renderer, texture_image,NULL, &dstrect); SDL_RenderPresent(renderer); } // end of program loop //------------------------------------------------- SDL_DestroyTexture(texture_image); SDL_FreeSurface(image); SDL_DestroyTexture(texture_hWnd_down); SDL_FreeSurface(hWnd_down); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; }
 
- 
					
					
					
					
 Anstatt [code]...[/code]mußt du hier Markdown benutzen, d.h. ```...``` (jeweils 3 Backticks) bzw. den Button</>benutzen.Dann schau ich mir deinen Code genauer an. 
 
- 
					
					
					
					
 @Th69 hab ich 
 
- 
					
					
					
					
 Du hast also arrayofPanel[i].image_pos_x += rand()%800+1;vs. arrayofPanel[i].image_pos_x + 10;? (Und äquivalent für y?) Letzteres tut nichts, da fehlt ein =, alsoarrayofPanel[i].image_pos_x += 10;Aktiviere beim Compilieren auch immer alle Warnungen (wie genau, kommt auf den Compiler an, aber kannst du herausfinden). Einen Ausdruck, der nichts tut, sollte jeder Compiler als Unsinn bemängeln. 
 
- 
					
					
					
					
 Meinst du Zeile 89 (mit +100)?+50sehe ich nirgendwo im Code.
 Ich kann so ersteinmal keinen logischen Fehler erkennen (außer daß dudstrect2.xdort unnötigerweise zweimal hintereinander zuweist, vorher schon in Zeile 83).Was genau meinst du denn mit "klappt das ganze nicht"? Wird nichts angezeigt oder stimmen die Koordinaten nicht? Und noch ein paar Hinweise: - srand(time(NULL))sollte man nur einmalig am Anfang des Programms (in- main()aufrufen), da es sonst in der Schleife jedesmal den Zufallszahlengenerator neu initialisiert (und innerhalb einer Sekunde dieselbe Zufallszahlen generiert).
- int quitsollte besser- bool quitsein (mit- falseund- true)
- in image_class::show(...)solltest du den Parametertexturebenutzen, anstatt die globale Variabletexture_image
- Diese Klassenfunktion sollte auch gar nicht auf das globale Array arrayofPanelzugreifen, sondern nur auf die Member der eigenen Klasseimage_class- ansonsten sollte dies eine freie Funktion sein (so daß du dann keine Dummy-VariablemyClassbenötigst).
 
 
- 
					
					
					
					
 @Th69 ich hab das oben im Code mal alles mal sauberer gemacht: Das passiert: https://ibb.co/nqPK9pwz 
 https://ibb.co/r2Yv1cnbOder das: 
 https://ibb.co/cRtGbqH
 https://ibb.co/hFZHv8xnDas käme am nächsten: 
 https://ibb.co/hxDZdqjd
 https://ibb.co/CKCxpxc0Aber auch doof 
 
- 
					
					
					
					
 Da passiert genau das, was du programmiert hast. Du hast wohl einen Denkfehler, denn ich denke, du willst die Rechtecke alle nebeneinander haben. 
 Bei deiner 2. Version initialisiert du allex-Werte des Arrays zuerst auf0, um dann alle jeweils um50zu erhöhen, d.h. du renderst alle anx-Position50.
 Am besten, du benutzt eine lokale Variablex, die du vor der Schleife auf0initialisierst, und diese dann in der Schleife jeweils um50erhöhst und dann dem Array-Element zuweist.
 Alternativ kannst du auch die Schleifenvariableials Multiplikator verwenden.PS: Ich hoffe, dir ist klar, daß du mit dem bisherigen Aufruf von myClass.show(renderer, texture_image,NULL, &dstrect)noch mal die gleichen Objekte renderst, wie in der Schleife zuvor (zumindestens, wenn du die auskommentierten Zuweisungen zudstrect.hunddstrect.wwieder mitkompilierst - wo du übrigensxundyvertauscht hast)?!Und deine beiden Kommentare bei #include <cstdlib> // this is for the classes #include <sstream> // and this for "arrayof"sind auch falsch. Dein genereller Codeaufbau erscheint mir auch viel zu C-lastig, anstatt C++ (auch wenn du bisher eine Klasse benutzt). 
 Anstatt SDL würde ich SFML empfehlen.Edit: Oder wenigstens einen C++ Wrapper für SDL2, wie z.B. libSDL2pp 
 
- 
					
					
					
					
 So, habe es jetzt mal anders "zusammen gegoogelt" TUTs  Hier: #include <SDL2/SDL.h> #include <stdio.h> #include <stdlib.h> #define hWnd_Button_height 48 #define hWnd_Button_width 42 SDL_Surface* image = NULL; SDL_Texture* texture_image = NULL; SDL_Rect dstrect; SDL_Rect rects[19][25]; //________________ SDL_Rect newSDL_Rect(int xs, int ys, int widths, int heights) { SDL_Rect rectangular; rectangular.x = xs; rectangular.y = ys; rectangular.w = widths; rectangular.h = heights; return rectangular; } //_________MAIN ____________ int main( int argc, char *argv[] ) { SDL_Event event; SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("SDL2 clone a surface ", 0, 0, 1024, 768, SDL_WINDOW_SHOWN); SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); //------------------------------------------------- image = SDL_LoadBMP("hwnd_blank_up.bmp"); texture_image = SDL_CreateTextureFromSurface(renderer,image); //---------------- SDL_Surface *hWnd_down = SDL_LoadBMP("hwnd_blank_down.bmp"); SDL_Texture *texture_hWnd_down = SDL_CreateTextureFromSurface(renderer,hWnd_down); //------------------------------------------------- int quit = 1; int system_var = 2; while(quit == 1) // prgramm loop { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = 0; break; } } //--------------- for (int i = 0; i < 18; i++) for (int j = 0; j < 24; j++) { rects[i][j] = newSDL_Rect(20 + i*42, 20 + j*42, 40, 40); SDL_SetRenderDrawColor(renderer, 255, 102, 0, 255); SDL_RenderFillRect(renderer, &rects[i][j]); } SDL_RenderPresent(renderer); } // end of program loop //------------------------------------------------- SDL_DestroyTexture(texture_image); SDL_FreeSurface(image); SDL_DestroyTexture(texture_hWnd_down); SDL_FreeSurface(hWnd_down); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; }
 
- 
					
					
					
					
 Dieser Beitrag wurde gelöscht!
 
 
			
			
		