Wednesday, February 13, 2019

Go Interfaces Tutorial

Typically, if you see a function or a method that expects an empty interface, then you can typically pass anything into this function/method.

Let's see first example main.go below:
package main

import (
 "fmt"
)

func myFunc(x interface{}) {
 fmt.Println(x)
}

func main() {
 var my_number int
 my_number = 50
 
 myFunc(my_number)
}

If we then go to run this then we should see that it runs successfully and prints out our integer value:

$ go run main.go
50


Interface is very useful, by defining a function that takes in an interface{}, we essentially give ourselves the flexibility to pass in anything we want. 
If we define a type based off this interface then we are forced to implement all of the functions or methods defined within that interface type.

Let's see second example main.go below:
package main

import "fmt"

type Sport interface {
 TypeSport()
}

type ChessSport struct {
 Name string
}

type FootballSport struct {
 Name string
}

func (b ChessSport ) TypeSport() {
 fmt.Printf("%s goes sport\n", b.Name)
}

func (b FootballSport ) TypeSport() {
 fmt.Printf("%s goes Football Sport\n", b.Name)
}

func main() {
 var athletes1 ChessSport
 athletes1.Name = "Joko"
 athletes1.TypeSport()

 var athletes2  FootballSport 
 athletes2.Name = "Budi"
 athletes2.TypeSport()
}

Should we wish, we could then create an array of type Sport which could store both our ChessSport and FootballSport objects.

var athletes []Sport
athletes = append(athletes , athletes1)
athletes = append(athletes , athletes2)

Hopefully, this article useful. Thank you 


Other Topics:

Tuesday, February 12, 2019

Emscripten and SDL2 Tutorial --- WebAssembly C++

I believe that most C/C++ programmers are very interested with WebAssembly. Emscripten provides a number of ways to solve the first problem of making files on the server accessible to C/C++ programs.

SDL (Simple DirectMedia Layer) is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

This is just to show on how SDL can be implemented into WebAssembly by displaying an image.


I hope you like it and let's get started
 
Environment Specification
I am using Ubuntu 16.04 LTS with standard build tools for C/C++.

If you need HelloWorld Tutorial, you can find here.


Writing Code

$ touch hello.cpp
 
 #include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <emscripten.h>
#include <unistd.h>
#include <stdlib.h>

int testImage(SDL_Renderer* renderer, const char* fileName)
{
  SDL_Surface *image = IMG_Load(fileName);
  if (!image)
  {
     printf("IMG_Load: %s\n", IMG_GetError());
     return 0;
  }
  int result = image->w;


  SDL_Rect dest = {.x = 200, .y = 100, .w = 200, .h = 200};

  SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, image);

  SDL_RenderCopy (renderer, tex, NULL, &dest);

  SDL_DestroyTexture (tex);

  SDL_FreeSurface (image);

  return result;
}

int main()
{
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window *window;
  SDL_Renderer *renderer;

  SDL_CreateWindowAndRenderer(600, 400, 0, &window, &renderer);

  int result = 0;

  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  SDL_RenderClear(renderer);

  result |= testImage(renderer, "golang.png");

  SDL_RenderPresent(renderer);

  printf("you should see an image.\n");

  return 0;
}


Compile It
$ emcc hello.c -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]'     --preload-file assets -o hello.html


Run It
$ emrun hello.html


Resut






Thank you


Other Topics: