Friday, May 19, 2023

Modularity

A well-designed program must be modular if it has a big size, and guess what, these projects are big. So, for this entry, we will be making a modular base for our program. This means, we want at least 3 modules (files):

  • main program

----------------------------------------------------------

// File: main. c

#include<SDL2/SDL.h>
#include<stdio.h>


int main (int argc, char* args[]) {

 initSDL();  

 // Main loop
 int keyPressed = 0;
 while (keyPressed >= 0) { // -1 == Quit button has been pressed
 
  keyPressed = checkUsrActionsKeyboard();
  sdlLoadsMedia();
 
 }

 closeSDL();


 return 0;
}

---------------------------------------------------------- 

 

  • Window, initialize and close SDL.

----------------------------------------------------------

// file: window.utils.sdl.c

#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>


// Screen dimension constants
const int SCREEN_WIDTH  = 960;
const int SCREEN_HEIGHT = 544;

// window rendered to
SDL_Window* gWindow = NULL;
// Surface contained
SDL_Surface* gScreenSurface = NULL;
// Image loaded and showed on screen
SDL_Surface* gIcePuck = NULL;

// Starts SDL and creates window
int initSDL() {

 // Initialize SDL
 if( SDL_Init(SDL_INIT_VIDEO) <0) {
  printf( "SDL couldn't initialize! SDL_ERROR: %s", SDL_GetError() );
  return -1;
 }
 // Creates Window
 else {
  gWindow = SDL_CreateWindow( "Ice_Puck", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  if( gWindow == NULL ) {
   printf("Window couldn't be created! SDL_Error: %s", SDL_GetError() );
   return -1;
  }
  else {
   // Get window surface
   gScreenSurface = SDL_GetWindowSurface( gWindow );
  }
 }
 return 0; // Successful init
}

// Loads media
int sdlLoadsMedia() {

   gScreenSurface = SDL_GetWindowSurface( gWindow );
   // Fill the surface White
   SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface -> format, 0xFF, 0xFF, 0xFF ) );
   // Update the surface
   SDL_UpdateWindowSurface( gWindow );

 return 0; // Successful SDL load
}

// Frees media and shuts down SDL
void closeSDL() {
 // Deallocate Surface
 SDL_FreeSurface( gIcePuck );
 gIcePuck = NULL;
 // Destroy window
 SDL_DestroyWindow( gWindow );
 gWindow = NULL;
 // Quit SDL subsystems
 SDL_Quit();
}

---------------------------------------------------------- 

 

  • keyboard controls

----------------------------------------------------------

// file: controls.utils.sdl.c

#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>

int checkUsrActionsKeyboard() {

 int keyFlag = 0;

 // Key event Handler
  SDL_Event e;

 // Event handling Loop
 while ( SDL_PollEvent( &e ) != 0 ) {
 
  // User requests to close window
  if ( e.type == SDL_QUIT ) {
   keyFlag = -1;
  }
 
  // User presses a key
  else if ( e.type == SDL_KEYDOWN ) {
   // Select action based on key press
   switch ( e.key.keysym.sym ) {

    case SDLK_UP:
    //break;


    default:
    //gCurrentSurface = gKeyPressSurfaces [ KEY_PRESS_SURFACE_DEFAULT ];
    break;
   }
  }
 }
 return keyFlag;
}

----------------------------------------------------------

 

  • We can add another module for the game-pad control later.
  • We'll also need a sound module, but that's not our priority for now.

 
*
Besides we have to create the header files: files with extension .h which contain function declarations and macro definitions to be shared between several source files.

 

And don't forget to add these files to the CMakeLists.txt

----------------------------------------------------------

cmake_minimum_required(VERSION 3.7)

project(IcePuck)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(IcePuck main.c window.utils.sdl.c controls.utils.sdl.c)
target_link_libraries(IcePuck ${SDL2_LIBRARIES})

----------------------------------------------------------

No comments:

Post a Comment