Render string on screen
This commit is contained in:
		
							parent
							
								
									868922c68e
								
							
						
					
					
						commit
						b1ab0f6d98
					
				
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @ -6,7 +6,7 @@ CC = g++ | ||||
| 
 | ||||
| #COMPILER_FLAGS specifies the additional compilation options we're using
 | ||||
| # -w suppresses all warnings
 | ||||
| COMPILER_FLAGS = -w | ||||
| COMPILER_FLAGS = -Wall -Wpedantic | ||||
| 
 | ||||
| #LINKER_FLAGS specifies the libraries we're linking against
 | ||||
| LINKER_FLAGS = -lSDL2 -lSDL2_image | ||||
|  | ||||
							
								
								
									
										151
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										151
									
								
								main.cpp
									
									
									
									
									
								
							| @ -21,68 +21,33 @@ enum KeyPressSurfaces | ||||
|     KEY_PRESS_SURFACE_TOTAL | ||||
| }; | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|     SDL_Window* window = NULL; | ||||
| 
 | ||||
|     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { | ||||
|         printf("error initializing SDL: %s\n", SDL_GetError()); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     window = SDL_CreateWindow( "SDL Tutorial", | ||||
|                                SDL_WINDOWPOS_UNDEFINED, | ||||
|                                SDL_WINDOWPOS_UNDEFINED, | ||||
|                                SCREEN_WIDTH, | ||||
|                                SCREEN_HEIGHT, | ||||
|                                SDL_WINDOW_SHOWN ); | ||||
|     if (window == NULL) { | ||||
|         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     SDL_Renderer* renderer = | ||||
|         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | ||||
|     SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1); | ||||
| 
 | ||||
| 
 | ||||
|     SDL_Surface* fontSurface = IMG_Load("font.png"); | ||||
|     if (fontSurface == NULL) { | ||||
|         cout << "Could not find spritesheet" << endl; | ||||
|         return 1; | ||||
|     } | ||||
|     SDL_Texture* fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface); | ||||
|     // SDL_Surface* screenSurface = NULL;
 | ||||
|     // //Get window surface
 | ||||
|     // screenSurface = SDL_GetWindowSurface( window );
 | ||||
| 
 | ||||
|     //Fill the surface white
 | ||||
|     // auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
 | ||||
| 
 | ||||
|     int test = 0; | ||||
|     SDL_Event e; | ||||
|     bool quit = false; | ||||
|     int keyPressed = -1; | ||||
|     while (quit == false) { | ||||
|         while (SDL_PollEvent( &e)) { | ||||
|             if (e.type == SDL_QUIT) { | ||||
|                 quit = true; | ||||
|             } | ||||
|             else if (e.type == SDL_KEYDOWN) { | ||||
|                 char key = e.key.keysym.sym; | ||||
|                 if (key >= (char)'!' || key <= (char)'~') { | ||||
|                     keyPressed = key; | ||||
|                 } | ||||
|                 // switch (e.key.keysym.sym) {
 | ||||
|                 //     case SDLK_
 | ||||
|                 // }
 | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // SDL_FillRect( screenSurface, NULL, rgbMap);
 | ||||
| 
 | ||||
| SDL_Rect get_srcrect(char key) { | ||||
|     SDL_Rect srcrect; | ||||
|     srcrect.x = (key % 16) * 70; | ||||
|     srcrect.y = (key / 16) * 80; | ||||
| 
 | ||||
|     srcrect.w = 80; | ||||
|     srcrect.h = 80; | ||||
| 
 | ||||
|     return srcrect; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void render_char(char key, int row, int col, SDL_Renderer* renderer, SDL_Texture* font) { | ||||
|     SDL_Rect dstrect; | ||||
| 
 | ||||
|     dstrect.x = col; | ||||
|     dstrect.y = row; | ||||
|     dstrect.w = 80; | ||||
|     dstrect.h = 80; | ||||
|      | ||||
|     SDL_Rect srcrect = get_srcrect(key); | ||||
|     SDL_RenderCopy(renderer, font, &srcrect, &dstrect); | ||||
| } | ||||
| 
 | ||||
| void Draw(char keyPressed, SDL_Renderer* renderer, SDL_Texture* fontTexture) { | ||||
|     SDL_Rect srcrect; | ||||
|     SDL_Rect dstrect; | ||||
| 
 | ||||
|     if (keyPressed > 0) { | ||||
|         srcrect.x = (keyPressed % 16) * 70; | ||||
| @ -105,10 +70,76 @@ int main(int argc, char *argv[]) { | ||||
|     //Update the surface
 | ||||
|     // SDL_UpdateWindowSurface( window );
 | ||||
| 
 | ||||
|         SDL_RenderClear(renderer); | ||||
| 
 | ||||
|     SDL_RenderCopy(renderer, fontTexture, &srcrect, &dstrect); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|     SDL_Window* window = NULL; | ||||
| 
 | ||||
|     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { | ||||
|         printf("error initializing SDL: %s\n", SDL_GetError()); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     window = SDL_CreateWindow( "SDL Tutorial", | ||||
|                                SDL_WINDOWPOS_UNDEFINED, | ||||
|                                SDL_WINDOWPOS_UNDEFINED, | ||||
|                                SCREEN_WIDTH, | ||||
|                                SCREEN_HEIGHT, | ||||
|                                SDL_WINDOW_SHOWN ); | ||||
|     if (window == NULL) { | ||||
|         printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | ||||
|     SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1); | ||||
| 
 | ||||
| 
 | ||||
|     SDL_Surface* fontSurface = IMG_Load("font.png"); | ||||
|     if (fontSurface == NULL) { | ||||
|         cout << "Could not find spritesheet" << endl; | ||||
|         return 1; | ||||
|     } | ||||
|     SDL_Texture* fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface); | ||||
|     // SDL_Surface* screenSurface = NULL;
 | ||||
|     // //Get window surface
 | ||||
|     // screenSurface = SDL_GetWindowSurface( window );
 | ||||
| 
 | ||||
|     //Fill the surface white
 | ||||
|     // auto rgbMap = SDL_MapRGB( screenSurface->format, 0x82, 0xA3, 0xFF);
 | ||||
| 
 | ||||
|     SDL_Event e; | ||||
|     bool quit = false; | ||||
|     int keyPressed = -1; | ||||
|     int rowCurr = 0; | ||||
|     int colCurr = 0; | ||||
|     string text = "Some text"; | ||||
|     while (quit == false) { | ||||
|         while (SDL_PollEvent( &e)) { | ||||
|             if (e.type == SDL_QUIT) { | ||||
|                 quit = true; | ||||
|             } | ||||
|             else if (e.type == SDL_KEYDOWN) { | ||||
|                 char key = e.key.keysym.sym; | ||||
|                 if (key >= (char)'!' || key <= (char)'~') { | ||||
|                     keyPressed = key; | ||||
|                 } | ||||
|                 // switch (e.key.keysym.sym) {
 | ||||
|                 //     case SDLK_
 | ||||
|                 // }
 | ||||
|             } | ||||
|         } | ||||
|         // SDL_FillRect( screenSurface, NULL, rgbMap);
 | ||||
|         SDL_RenderClear(renderer); | ||||
|         SDL_SetTextureColorMod(fontTexture, 0xFF, 0, 0); | ||||
|         for (int i = 0; i < 10; i++) { | ||||
|             render_char(text[i], rowCurr, colCurr++ * 50, renderer, fontTexture); | ||||
|         } | ||||
|         colCurr = 0; | ||||
|         // Draw(keyPressed, renderer, fontTexture);
 | ||||
|         SDL_RenderPresent(renderer); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user