nssilva / XST

simple library for 2d game canvas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

XST3 simple library for 2d game canvas

Version 0.3 Beta

License and copyright

Copyright (c) 2017 Nelson Silva

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Quick manual

Opens a canvas with the width, height and in fullscreen = true or false and a title optional.

Example:

    winScreen(640, 480, false, 'title');

if no width or height provided, winScreen()-> the default would be 640 to 480 no fullscreen and Untitled as a title .

Set screen title.
this can be done in winScreen or with a separeted function called setTitle().

If screen is in full mode then this title goes to the tab.

Example:

    setTitle('Fractal');

Sets a font to the size and name given.

Example:

    setFont(size, name);

Gets the font.

Example:

    setFont(28, 'Algerian');
    var font = getFont();

This is the main loop function, if you need to repeat the code you must use the main function.

Example:

	function main() {
		drawtext("Mouse X: "+ getmouseX(), 10, 20);
	}

Writes text to the screen at a given x and y coords.

Example:

    drawText("Screen X: "+ screenWidth(), 10, 20);

Clears the screen, most used in main loop. (color is only used when we need alpha or something else like changing background color...)

Example:

    clear();
    clear(rgb(255,255,255));

circle(x, y, radius, bordercolor); buils a circle in x, y with a radius and a border color

example: circle(100, 100, 10, rgb(0,255,0));

fillCircle(x, y, radius, bordercolor, fillcolor) the same as above but filled

example: fillCircle(150, 100, 60, rgb(0,255,0));

line(x1, y1, x2, y2, linewidth, color) rect(linewidth, bordercolor, x, y, width, height) fillRect(x, y, width, height, color) rgb(r, g, b) setColor(color)->in rgb() format setBackColor(rgb(0,0,0)) -> sets the backcolor of canvas loadTexture(path)->path to image

Example:

    var player = loadTexture("images/player.png");

drawTexture draws an image in the screen at given coords.

example:

    drawTexture(player, 100, 200);

getMouseX() getMouseY() getMouseButton() getMouseState()

Example: var mouse = getMouseState(); mouse.x, mouse.y, mouse.RMB, mouse.LMB

getKey() getImageWidth(image) getImageHeight(image) getScreenHeight() getScreenWidth() Entitie(type, image, x, y)

How to use entities. Example of a player like above but registered as entitie.

Example: var player = new Entitie('player', 'images/player.png', 100, 100); var enemie = new Entitie('enemie', 'images/enemoie.png', 0, 0)

drawTexture(player.image, player.x, player.y)

keys: (MOST IMPORTANT)

  • BACKSPACE
  • TAB
  • ENTER
  • PAUSE
  • CAPS
  • ESC
  • K_SPACE
  • PAGE_UP
  • PAGE_DOWN
  • END
  • HOME
  • K_LEFT
  • K_UP
  • K_RIGHT
  • K_DOWN
  • INSERT
  • DELETE
String functions
left$(text, num)
len$(params)
mid$(text, start, num)
right$(text, num)
trim$()
Math functions
abs(params)   
acos(params)  
asin(params)  
atan(params)  
cos(params)   
exp(params)   
log(params)   
rnd(params)   
round(params) 
sin(params)
sqr(params)
tan(params)  
int(params)
val(params
distance(x1, y1, x2, y2)
dec2bin(dec)
bin2dec(bin)

Screens

Fern Julia

Code for the julia

winScreen(640, 480, false);
setTitle('Julia Set');
var zoom = 1;
var moveX = 0;
var moveY = 0;
var maxIterations = 300;

var cRe = -0.7;
var cIm = 0.27015;
var H = getScreenHeight();
var W = getScreenWidth();

for (var y = 0; y < H; y++) {
	for (var x = 0; x < W; x++) {
		var newRe = 1.5 * (x - W / 2) / (0.5 * zoom * W) + moveX
		var newIm = (y - H / 2) / (0.5 * zoom * H) + moveY
		for (var i = 0; i < maxIterations; i++) {
		    var oldRe = newRe;
		    var oldIm = newIm;
		    newRe = (oldRe * oldRe) - (oldIm * oldIm) + cRe;
		    newIm = (2 * oldRe * oldIm) + cIm;
		    if (((newRe * newRe) + (newIm * newIm)) > 4) { break; }
		}
		var col = setHSVToRGB(setColorHSV(i % 256, 255, 255 * (i < maxIterations)));
		drawPixel(x, y, rgb(col.r, col.g, col.b));
	}
}

To see some examples and demos check this blog.

programmingrandomstuff


Author

  • Nelson Silva

About

simple library for 2d game canvas


Languages

Language:JavaScript 99.6%Language:HTML 0.4%