Wednesday, March 1, 2023

Back to the Basics

 After more than two years, I've come back here, with the original idea still as hot as the first day. However, the approach of this entry is to create a new generic sample, so it can be checked in any system quickly.

Doing this, we can have something (similar to an SDL hello world) to test, without any Vita or SDK requirement. Besides, we can use it as a base for everything else.


First, we'll need to create our main.c file
(check https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php for more info)

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

// File: main. c

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


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

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

 // Rendered Window
 SDL_Window* window = NULL;
 // Surface Contained by the Window
 SDL_Surface* screenSurface = NULL;
 // Initialize SDL
 if ( SDL_Init(SDL_INIT_VIDEO) < 0) {
  printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
 }
 else { // Create window
   window = SDL_CreateWindow( "Ice_Puck",
   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() );
  }
  else { // Get window surface
   screenSurface = SDL_GetWindowSurface( window );
   // Fill the surface White
   SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface -> format, 0xFF, 0xFF, 0xFF ) );
   // Update the surface
   SDL_UpdateWindowSurface( window );
   // Keep the window up
   SDL_Event e;
   bool quit = false;
   while (quit == false) {
    while ( SDL_PollEvent ( &e ) ) {
     if (e.type == SDL_QUIT) quit = true;
    }
   }
  }
 }
 // Destroy Window
 SDL_DestroyWindow(window);
 // Quit SDL subsystems
 SDL_Quit();

 return 0;
}

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

 After that, it's time to create a CMakeLists.txt
(source: https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/)

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

cmake_minimum_required(VERSION 3.7)

project(SDL2Test)

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

add_executable(SDL2Test main.c)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})

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

And finally, with just those two files, we can build our sample:

go to your shell of choice and write the following commands:

# If we didn't have SDL and SDL_IMAGE on the computer
# we have to install them
sudo apt-get update
sudo apt-get install libsdl2-dev
sudo apt-get install libsdl2-image-dev


# Buildsystems Generation (produce a makefile for the project)
cmake .  

# Buildsystem execution (creates the executable)
make

# Give executable permissions to the file created
chmod +x SDL2Test 

# Run the image and check your results
./SDL2Test


####################
            Additional notes
           to use SDL_image
                 for psVita
####################

Clone or download the repo from
https://github.com/rsn8887/SDL_image/

Go to your shell of choice and write the following commands:

# Rename Makefile.psp2
mv Makefile.vita Makefile

# Install SDL_IMAGE
make install

Besides, you will need to make this changes into your project's cmake file
project(name of the project)
include("${VITASDK}/share/vita.cmake" REQUIRED)
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)


No comments:

Post a Comment