Tuesday, July 14, 2020

Basiscs on the code PART 4

This entry is about the sld2 sample. As the name suggests, it uses the sld2 library.

The sample deeps in drawing pictures in the screen (rendering), specificly a red rectangle as its name implies.

To understand the functions, we have to use the vitasdk documentation, for this specific sample we will use the graphics section. In addition, we need to use the sdl2 wiki.

Note: Again, the sample doesn't use any specific function from the vita, appart from the "includes" and "defines" to be executed. However, we think it is useful to have the vitasdk documentation stated here as well.

Note 2: For those who need this, it uses the RGB color code, you can find the different codes here.


 SAMPLE "sdl/redrectangle"

redrectangle: Example SDL rendering.


// this time the code is pretty forward and you can see some features of the vita graphics so, we will put a small number of comments here.

#include <psp2/kernel/processmgr.h>
#include <SDL2/SDL.h>

//Screen dimension constants
enum {
  SCREEN_WIDTH  = 960,
  SCREEN_HEIGHT = 544
};

// Part from the sdl library to declare constant and variables

SDL_Window    * gWindow   = NULL;
SDL_Renderer  * gRenderer = NULL;

SDL_Rect fillRect = { SCREEN_WIDTH  / 4,
              SCREEN_HEIGHT / 4,
              SCREEN_WIDTH  / 2,
              SCREEN_HEIGHT / 2
};


// main program

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

// sdl init and checking if has had any error

  if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
      return -1;

// creating the rectangle and checking if has been any errors

  if ((gWindow = SDL_CreateWindow( "RedRectangle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN)) == NULL)
    return -1;

// rendering the figures and again checking errors

  if ((gRenderer = SDL_CreateRenderer( gWindow, -1, 0)) == NULL)
      return -1;

// actually drawing the figures and colors in the screen

  SDL_SetRenderDrawColor( gRenderer, 255,0,0,255);
  SDL_RenderFillRect( gRenderer, &fillRect );
  SDL_RenderPresent( gRenderer );

  SDL_Delay(4000);

// cleaning the memory

  SDL_DestroyRenderer( gRenderer );
  SDL_DestroyWindow( gWindow );
  gWindow = NULL;
  gRenderer = NULL;
 
// exiting

  SDL_Quit(); // close the sdl
  sceKernelExitProcess(0); // close the vita proccess runing, usually close the app
  return 0;
}

-----------

Screenshot from the sample running on the vita:





And that's all for this sample.

No comments:

Post a Comment