Advertising (This ad goes away for registered users. You can Login or Register)

[DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, World

Post here your guides, tips, how-to, etc...
thevitamaster
Posts: 10
Joined: Tue Aug 16, 2016 5:49 am

[DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, World

Post by thevitamaster »

Hi guys,
welcome to part one of my new tutorial series on programming for the Vita, where we will be laying the groundwork for you to develop your own homebrew / port games to the vita! Today we will be doing a graphical hello world with the unofficial VitaSDK and vita2dlib.

Requirements:

- Vita with Henkaku
********************************************************************************************************************
- Unofficial VitaSDK and vita2dlib installed on a linux system (see mylast tutorial)
********************************************************************************************************************
- Local Area Network connectivity between Vita and linux system
********************************************************************************************************************

So, don't be scared but here comes a dump of code. Scroll a little bit down for the explanation.
Main.c

Code: Select all

#include <psp2/display.h>
#include <psp2/moduleinfo.h>
#include <psp2/kernel/processmgr.h>

#include <vita2d.h>

PSP2_MODULE_INFO(0, 0, "HelloWorld");

int main() {
	
	vita2d_pgf *pgf;

	vita2d_init();
	vita2d_set_clear_color(RGBA8(0, 0, 0, 255));

	pgf = vita2d_load_default_pgf();
	
	while(1){
		vita2d_start_drawing();
		vita2d_clear_screen();
	
		vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0, 255, 0, 255), 1.0f, "Hello, World!");
		
		vita2d_end_drawing();
		vita2d_swap_buffers();
	}
	
	vita2d_fini();
	vita2d_free_pgf(pgf);
	sceKernelExitProcess(0);
	return 0;
}
Explanation of main.c (above):
First of all, let's look at the include directives (for example the "#include <psp2/display.h>" directive).
These include directives, also called preprocessor directives, because they are enumerated into files on a "preparation" pass, basically just tell the C compiler to include (e.g. copy) the contents of this header file (display.h) into the file being compiled (our file main.c in this case). With our Vita, also called an embedded linux device, we use these include directives to use functions specific to some aspect of the hardware / software. For example, if we do "#include <psp2/ctrl.h>", we want access to controller-related functions, variables, enums (a new variable type documented in the header and defined in source), and so forth, so we can for example do a bitwise AND ("&" in C) with our own subset variable of type SceCtrlData called "pad.buttons" and the controller state of the UP directional button, which is called "SCE_CTRL_UP".
********************************************************************************************************************
Now, before we dive into the skeletal function calls of a Vita program, we have to define how a function routine is created / defined in C and also, how to then call it. So, let's assume we have a system capable of running assembled C code in a stable manner as well as capable of printing printf() calls to the standard output, which is presumptuously a terminal. Here's how the creation and calling of a function in the same file would work:
example.c:

Code: Select all

#include <stdio.h>

// Creation of function routine
int printdec(int decimal){
printf("%d", decimal);
return 0;
}

int main(){
// Function call
printdec(123);
return 0;
}
So, now we can see that function creation and function call always follow a pattern. The pattern for function creation is:

Code: Select all

[variable type you want to return] [function name]([function arguments]){
[routine]
}
On the other hand, the pattern for function calling is:

Code: Select all

[function name]([function arguments]);
Note the semicolon at the end of every function call, variable assignment, defintion, ... as this tells the C compiler that the instruction ends there.
********************************************************************************************************************
Now we can look at the first skeletal function call in Vita program, namely

Code: Select all

PSP2_MODULE_INFO(0, 0, "HelloWorld");
This function is defined in psp2/moduleinfo.h and, as you can see, it gives the Vita system some basic information on our program, namely
  1. 1. The attribute (No idea what that is)
  • 2. The version of your program
  • 3. Your program name. This has a maximum length of 27 characters.
We pass this information in said order in the function arguments of PSP2_MODULE_INFO.
********************************************************************************************************************
Next up:

Code: Select all

int main(){
In the C language, as opposed to say Python 2.7, every function call that should be executed has to be in the routine of the main() function. So, basically, every function call we want to make, we put within the "{" curly braces following "int main()".
********************************************************************************************************************
Following, we have:

Code: Select all

vita2d_pgf *pgf;
This variabble defintion basically creates a new pointer variable of type vita2d_pgf which we can assign to the start of a pgf file with the appropriate function call.
********************************************************************************************************************

Code: Select all

vita2d_init();
With that function call, we initialize the vita2d graphics engine.
********************************************************************************************************************

Code: Select all

vita2d_set_clear_color(RGBA8(0, 0, 0, 255));
Woah, that's quite something to dissect, but we'll get it done.
So, first of all, we have the function call vita2d_set_clear_color() which sets the color the vita should display when the screen is cleared of all to be rendered elements.
Inside that, as the colour argument, we have the RGBA8() macro, which is defined (we'll get to that in part 2) in vita2dlib. Basically, this macro allows you to specify colours.
Here, we can specify the Red, Green, Blue and Alpha values, the max of every single value being 255, and the min 0.
The Red value specifies the amount of red, the Green value the amount of green and so on.
But what does the alpha value specify??
Well, that value specifies the opacity of the object, so how little transparency there is: 255 is no transparency and 0 is all transparent.
********************************************************************************************************************

Code: Select all

pgf = vita2d_load_default_pgf();
This is a variable assignment. Here, we assign the return value of vita2d_load_default_pgf() to the pointer pgf of type vita2d_pgf.
********************************************************************************************************************

Code: Select all

while(1){
Here, we have an infinite while loop. We do this to draw the values all the time and not draw them once and then exit.
********************************************************************************************************************

Code: Select all

vita2d_start_drawing();
Here, we tell the vita to start the rendering process in memory not allocated to the display.
[!Important]But, we won't see anything on the vita screen until we call vita2d_swap_buffers();[/!Important]
Because then, our instructions will be swapped to the vita memory allocated to the display and the display controller will then execute the instructions.
********************************************************************************************************************

Code: Select all

vita2d_clear_screen();
We set the vita screen to display all black.
Remember vita2d_set_clear_color() and RGBA8()?
********************************************************************************************************************

Code: Select all

vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0, 255, 0, 255), 1.0f, "Hello World!");
So, this function call let's us draw text on the screen of the vita!
The arguments to the function are in following order:
  1. 1. Assigned pointer of type vita2d_pgf
  • 2. X coordinate of start of text
  • 3. Y coordinate of start of text
  • 4. Colour value created with the RGBA8 macro
  • 5. Value for the size of text of type float (e.g with decimal points)
  • 6. String to display
To 3. and 4.: Both of these values must be of type int (e.g. no decimal points).
********************************************************************************************************************

Code: Select all

vita2d_end_drawing();
End the creation of the graphical elements in memory; the memory can now be swapped.
********************************************************************************************************************

Code: Select all

vita2d_swap_buffers();
Swap the new drawn memory with the old one.
********************************************************************************************************************

Code: Select all

}
End the infinte while loop.
********************************************************************************************************************

Code: Select all

vita2d_fini();
Start all the cleanup processes, etc. of the vita2d engine.
********************************************************************************************************************

Code: Select all

vita2d_free_pgf(pgf);
Free the memory allocated to the pgf font.
********************************************************************************************************************

Code: Select all

sceKernelExitProcess(0);
Exit the program using a function created in the unofficial VitaSDK.
********************************************************************************************************************

Code: Select all

return 0;
Because our main function is of type int, we return the value 0.
This is standard programming practice in C.

Makefile:

Code: Select all

TITLE_ID = VITA2DTST
TARGET   = vita2dsample
OBJS     = main.o

LIBS = -lvita2d -lSceKernel_stub -lSceDisplay_stub -lSceGxm_stub \
	-lSceSysmodule_stub -lSceCtrl_stub -lScePgf_stub \
	-lSceCommonDialog_stub -lfreetype -lpng -ljpeg -lz -lm -lc

PREFIX  = arm-vita-eabi
CC      = $(PREFIX)-gcc
CFLAGS  = -Wl,-q -Wall -O3
ASFLAGS = $(CFLAGS)

all: $(TARGET).vpk

%.vpk: eboot.bin
	vita-mksfoex -s TITLE_ID=$(TITLE_ID) "$(TARGET)" param.sfo

eboot.bin: $(TARGET).velf
	vita-make-fself $< $@

%.velf: %.elf
	vita-elf-create $< $@

$(TARGET).elf: $(OBJS)
	$(CC) $(CFLAGS) $^ $(LIBS) -o $@

%.o: %.png
	$(PREFIX)-ld -r -b binary -o $@ $^

clean:
	@rm -rf $(TARGET).vpk $(TARGET).velf $(TARGET).elf $(OBJS) \
		eboot.bin param.sfo
This "just works".

Compilation of our Hello World program:
1. Copy the code of main.c and Makefile and put them in two seperate files with their equivalent names.
2. Open a terminal, change your directory to that of the two files and run:

Code: Select all

make
Installation:
1. Open molecularshell on your vita and press select
2. Open a terminal and type:

Code: Select all

cd ~
wget https://github.com/xyzz/Vita_Doom/releases/download/1.0/vitadoom.vpk
ftp [ip displayed on your vita] [port displayed on your vita] (without the brackets)
put ~/vitadoom.vpk /ux0:vitadoom.vpk
3. Press circle on your vita and navigate to ux0:, then press cross when the vitadoom.vpk file is highlighted
4. Press select again
5. Open a terminal and type:

Code: Select all

ftp [ip displayed on your vita] [port displayed on your vita] (without the brackets)
put path/to/eboot.bin /ux0:app/DOOM00000/eboot.bin
put path/to/param.sfo /ux0:app/DOOM00000/sce_sys/param.sfo
6. Close molecularshell and open the Doom bubble
7. ???
8. Profit!!!

See you in the next part!
Advertising
Last edited by thevitamaster on Wed Aug 24, 2016 10:06 am, edited 1 time in total.
rodpin
Posts: 29
Joined: Wed May 09, 2012 11:22 am

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by rodpin »

Great tutorial! Good to start programming!
Advertising
thevitamaster
Posts: 10
Joined: Tue Aug 16, 2016 5:49 am

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by thevitamaster »

rodpin wrote:Great tutorial! Good to start programming!
Thanks for the feedback! Always great to hear the community's opinion :D
Abdulec
Posts: 46
Joined: Sun Aug 03, 2014 3:56 pm
Location: Algeria

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by Abdulec »

Whoa ! That's great ! I am a beginer on programming , I started learning C language basics so I cant really do something like creating vita homebrews , I wish u can give me some advices since u re skilled :D if that doesnt bother u send me a PM
PS VITA 3.60 HENkaku
I am interrested in programming I would like some one to advise me
Tutorials:viewtopic.php?f=65&t=42180
viewtopic.php?f=65&t=39320&hilit=Ps+vita+remote+desktop
xtc2014
Posts: 6
Joined: Thu Jan 30, 2014 3:32 am

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by xtc2014 »

Thanks for this fantastic tutorial that you put together. However, you forgot one extra line in the Makefile that would create the vpk file:

vita-pack-vpk -s param.sfo -b eboot.bin $@

The Makefile will look something like this once you add this line:

Code: Select all

TITLE_ID = VITA2DIST
TARGET   = vita2dsample
OBJS     = main.o

LIBS = -lvita2d -lSceKernel_stub -lSceDisplay_stub -lSceGxm_stub \
   -lSceSysmodule_stub -lSceCtrl_stub -lScePgf_stub \
   -lSceCommonDialog_stub -lfreetype -lpng -ljpeg -lz -lm -lc

PREFIX  = arm-vita-eabi
CC      = $(PREFIX)-gcc
CFLAGS  = -Wl,-q -Wall -O3
ASFLAGS = $(CFLAGS)

all: $(TARGET).vpk

%.vpk: eboot.bin
	vita-mksfoex -s TITLE_ID=$(TITLE_ID) "$(TARGET)" param.sfo
	vita-pack-vpk -s param.sfo -b eboot.bin $@

eboot.bin: $(TARGET).velf
	vita-make-fself $< $@

%.velf: %.elf
	vita-elf-create $< $@

$(TARGET).elf: $(OBJS)
	$(CC) $(CFLAGS) $^ $(LIBS) -o $@

%.o: %.png
	$(PREFIX)-ld -r -b binary -o $@ $^

clean:
	@rm -fr $(TARGET).vpk $(TARGET).velf $(TARGET).elf $(OBJS) \
      eboot.bin param.sfo
I can't wait for more tutorials from you in the future 8-)

NOTE: I think you might want to change the Installation section of the tutorial since it mentions about installing vitadoom.vpk instead of the program you created: vita2dsample.vpk
thevitamaster
Posts: 10
Joined: Tue Aug 16, 2016 5:49 am

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by thevitamaster »

xtc2014 wrote:Thanks for this fantastic tutorial that you put together. However, you forgot one extra line in the Makefile that would create the vpk file:

vita-pack-vpk -s param.sfo -b eboot.bin $@

The Makefile will look something like this once you add this line:

Code: Select all

TITLE_ID = VITA2DIST
TARGET   = vita2dsample
OBJS     = main.o

LIBS = -lvita2d -lSceKernel_stub -lSceDisplay_stub -lSceGxm_stub \
   -lSceSysmodule_stub -lSceCtrl_stub -lScePgf_stub \
   -lSceCommonDialog_stub -lfreetype -lpng -ljpeg -lz -lm -lc

PREFIX  = arm-vita-eabi
CC      = $(PREFIX)-gcc
CFLAGS  = -Wl,-q -Wall -O3
ASFLAGS = $(CFLAGS)

all: $(TARGET).vpk

%.vpk: eboot.bin
	vita-mksfoex -s TITLE_ID=$(TITLE_ID) "$(TARGET)" param.sfo
	vita-pack-vpk -s param.sfo -b eboot.bin $@

eboot.bin: $(TARGET).velf
	vita-make-fself $< $@

%.velf: %.elf
	vita-elf-create $< $@

$(TARGET).elf: $(OBJS)
	$(CC) $(CFLAGS) $^ $(LIBS) -o $@

%.o: %.png
	$(PREFIX)-ld -r -b binary -o $@ $^

clean:
	@rm -fr $(TARGET).vpk $(TARGET).velf $(TARGET).elf $(OBJS) \
      eboot.bin param.sfo
I can't wait for more tutorials from you in the future 8-)

NOTE: I think you might want to change the Installation section of the tutorial since it mentions about installing vitadoom.vpk instead of the program you created: vita2dsample.vpk
Thanks for pointing out the error :). I'll use the updated Makefile in the next tutorial.
Regarding the installation of vitadoom.vpk: We installed this so that we could just drag and drop our compiled eboot.bin & param.sfo by replacing those of vitadoom, but I get your point if I use the new Makefile
AlexxLopaztico02
Posts: 48
Joined: Thu Jul 21, 2016 9:30 pm

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by AlexxLopaztico02 »

Im voting for you in the monthly tutorial :D
thevitamaster
Posts: 10
Joined: Tue Aug 16, 2016 5:49 am

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by thevitamaster »

AlexxLopaztico02 wrote:Im voting for you in the monthly tutorial :D
Thanks AlexxLopaztico02 :D. If you want to continue learning how to program for the Vita, my next tutorial on interfacing with the controls and creating shapes moveable by the player has been released: [DEV TUT] Vita Programming Tutorials: Part 2 -- Controller
CPUzX
Moderator
Posts: 276
Joined: Thu Nov 28, 2013 8:25 pm
Location: United Kingdom

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by CPUzX »

Did not know a noob-friendly tutorial like this had existed for an explanation on programming the Vita. This is an awesome guide. Will be really helpful. I'm currently attempting to start learning C and everything else involved with programming the Vita, so this tutorial will be very useful. Thanks for your contribution!
RY0M43CH1Z3N
Posts: 10
Joined: Wed Dec 20, 2017 3:42 pm

Re: [DEV TUT] Vita Programming Tutorials: Part 1 -- Hello, W

Post by RY0M43CH1Z3N »

Triying to compile and...

Makefile:17: *** missing separator. Alto.
Locked

Return to “Tutorials”