Implement a basic scene class
This commit is contained in:
		
							parent
							
								
									bb07d62bc4
								
							
						
					
					
						commit
						efcf9cfffe
					
				
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @ -3,7 +3,7 @@ CC=clang++ | |||||||
| all: build run | all: build run | ||||||
| 
 | 
 | ||||||
| build: | build: | ||||||
| 	$(CC) -lSDL2 main.cpp -o breakout | 	$(CC) -lSDL2 src/scene.cpp src/breakout.cpp -o breakout | ||||||
| 
 | 
 | ||||||
| run: | run: | ||||||
| 	./breakout | 	./breakout | ||||||
|  | |||||||
| @ -1,17 +1,7 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include "SDL2/SDL.h" | #include "SDL2/SDL.h" | ||||||
| 
 | #include "breakout.h" | ||||||
| #define SCREEN_X 100 | #include "scene.h" | ||||||
| #define SCREEN_Y 100 |  | ||||||
| #define SCREEN_WIDTH 640 |  | ||||||
| #define SCREEN_HEIGHT 480 |  | ||||||
| #define PAD_WIDTH 200 |  | ||||||
| #define PAD_HEIGHT 20 |  | ||||||
| 
 |  | ||||||
| #define KEY_MOVE_STEP 5 |  | ||||||
| 
 |  | ||||||
| #define BG_COLOR 0, 0, 0, 255 |  | ||||||
| #define PAD_COLOR 0, 0, 255, 255 |  | ||||||
| 
 | 
 | ||||||
| int main(int argc, char const *argv[]) { | int main(int argc, char const *argv[]) { | ||||||
|     if (SDL_Init(SDL_INIT_VIDEO) != 0) { |     if (SDL_Init(SDL_INIT_VIDEO) != 0) { | ||||||
| @ -43,31 +33,14 @@ int main(int argc, char const *argv[]) { | |||||||
|         return 1; |         return 1; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Setting background color to black
 |     Scene scene(window, renderer); | ||||||
|     SDL_SetRenderDrawColor(renderer, BG_COLOR); |  | ||||||
|     SDL_RenderClear(renderer); |  | ||||||
| 
 |  | ||||||
|     // Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
 |  | ||||||
|     SDL_Rect pad = { |  | ||||||
|         .x = (SCREEN_WIDTH - PAD_WIDTH) / 2, |  | ||||||
|         .y = (SCREEN_HEIGHT - PAD_HEIGHT), |  | ||||||
|         .w = PAD_WIDTH, |  | ||||||
|         .h = PAD_HEIGHT |  | ||||||
|     }; |  | ||||||
| 
 |  | ||||||
|     // Set render color to blue ( rect will be rendered in this color )
 |  | ||||||
|     SDL_SetRenderDrawColor(renderer, PAD_COLOR); |  | ||||||
| 
 |  | ||||||
|     // Render rect
 |  | ||||||
|     SDL_RenderFillRect(renderer, &pad); |  | ||||||
| 
 |  | ||||||
|     // Render the rect to the screen
 |  | ||||||
|     SDL_RenderPresent(renderer); |  | ||||||
| 
 | 
 | ||||||
|     SDL_Event e; |     SDL_Event e; | ||||||
|     bool quit = false; |     bool quit = false; | ||||||
|     while (!quit) { |     while (!quit) { | ||||||
|  |         bool has_event = false; | ||||||
|         while (SDL_PollEvent(&e)) { |         while (SDL_PollEvent(&e)) { | ||||||
|  |             has_event = true; | ||||||
|             if (e.type == SDL_QUIT) { |             if (e.type == SDL_QUIT) { | ||||||
|                 quit = true; |                 quit = true; | ||||||
|             } |             } | ||||||
| @ -77,32 +50,24 @@ int main(int argc, char const *argv[]) { | |||||||
|                     quit = true; |                     quit = true; | ||||||
|                     break; |                     break; | ||||||
|                 case SDLK_LEFT: |                 case SDLK_LEFT: | ||||||
|                     pad.x -= KEY_MOVE_STEP; |                     scene.move_pad_relative(-KEY_MOVE_STEP); | ||||||
|                     break; |                     break; | ||||||
|                 case SDLK_RIGHT: |                 case SDLK_RIGHT: | ||||||
|                     pad.x += KEY_MOVE_STEP; |                     scene.move_pad_relative(KEY_MOVE_STEP); | ||||||
|                     break; |                     break; | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             if (e.type == SDL_MOUSEMOTION) { |             if (e.type == SDL_MOUSEMOTION) { | ||||||
|                 pad.x = e.motion.x - PAD_WIDTH / 2; |                 scene.move_pad(e.motion.x - PAD_WIDTH / 2); | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (pad.x < 0) { |  | ||||||
|                 pad.x = 0; |  | ||||||
|             } else if (pad.x > SCREEN_WIDTH - PAD_WIDTH) { |  | ||||||
|                 pad.x = SCREEN_WIDTH - PAD_WIDTH; |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // Re-rendering
 |         if (has_event) { | ||||||
|         SDL_SetRenderDrawColor(renderer, BG_COLOR); |             scene.render(); | ||||||
|         SDL_RenderClear(renderer); |         } else { | ||||||
|         SDL_SetRenderDrawColor(renderer, PAD_COLOR); |  | ||||||
|         SDL_RenderFillRect(renderer, &pad); |  | ||||||
|         SDL_RenderPresent(renderer); |  | ||||||
|             SDL_Delay(1); |             SDL_Delay(1); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     SDL_DestroyWindow(window); |     SDL_DestroyWindow(window); | ||||||
|     SDL_Quit(); |     SDL_Quit(); | ||||||
							
								
								
									
										6
									
								
								src/breakout.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/breakout.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | |||||||
|  | #define SCREEN_X 300 | ||||||
|  | #define SCREEN_Y 100 | ||||||
|  | #define SCREEN_WIDTH 400 | ||||||
|  | #define SCREEN_HEIGHT 600 | ||||||
|  | 
 | ||||||
|  | #define KEY_MOVE_STEP 5 | ||||||
							
								
								
									
										38
									
								
								src/scene.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/scene.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | |||||||
|  | #include <iostream> | ||||||
|  | #include "SDL2/SDL.h" | ||||||
|  | #include "breakout.h" | ||||||
|  | #include "scene.h" | ||||||
|  | 
 | ||||||
|  | Scene::Scene(SDL_Window *w, SDL_Renderer *r) { | ||||||
|  |     window = w; | ||||||
|  |     renderer = r; | ||||||
|  | 
 | ||||||
|  |     pad.x = (SCREEN_WIDTH - PAD_WIDTH) / 2; | ||||||
|  |     pad.y = (SCREEN_HEIGHT - PAD_HEIGHT); | ||||||
|  |     pad.w = PAD_WIDTH; | ||||||
|  |     pad.h = PAD_HEIGHT; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Scene::~Scene() {} | ||||||
|  | 
 | ||||||
|  | void Scene::render() { | ||||||
|  |     SDL_SetRenderDrawColor(renderer, BG_COLOR); | ||||||
|  |     SDL_RenderClear(renderer); | ||||||
|  |     SDL_SetRenderDrawColor(renderer, PAD_COLOR); | ||||||
|  |     SDL_RenderFillRect(renderer, &pad); | ||||||
|  |     SDL_RenderPresent(renderer); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Scene::move_pad(int x) { | ||||||
|  |     pad.x = x; | ||||||
|  | 
 | ||||||
|  |     if (pad.x < 0) { | ||||||
|  |         pad.x = 0; | ||||||
|  |     } else if (pad.x > SCREEN_WIDTH - PAD_WIDTH) { | ||||||
|  |         pad.x = SCREEN_WIDTH - PAD_WIDTH; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Scene::move_pad_relative(int delta) { | ||||||
|  |     move_pad(pad.x + delta); | ||||||
|  | } | ||||||
							
								
								
									
										21
									
								
								src/scene.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/scene.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | |||||||
|  | #define PAD_WIDTH 200 | ||||||
|  | #define PAD_HEIGHT 20 | ||||||
|  | 
 | ||||||
|  | #define BG_COLOR 0, 0, 0, 255 | ||||||
|  | #define PAD_COLOR 0, 0, 255, 255 | ||||||
|  | 
 | ||||||
|  | class Scene { | ||||||
|  | public: | ||||||
|  |     Scene(SDL_Window *w, SDL_Renderer *r); | ||||||
|  |     ~Scene(); | ||||||
|  | 
 | ||||||
|  |     void render(); | ||||||
|  |     void move_pad(int x); | ||||||
|  |     void move_pad_relative(int delta); | ||||||
|  | private: | ||||||
|  |     SDL_Window *window; | ||||||
|  |     SDL_Renderer *renderer; | ||||||
|  | 
 | ||||||
|  |     SDL_Rect pad; | ||||||
|  |     SDL_Rect blocks[10]; | ||||||
|  | }; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user