SGL基本思路讲解

SGL图形库是为Windows图形界面编程服务的,而且一切都是考虑到对新手友好的。在具体介绍提供给用户的函数之前,需要先说明一下应该以什么样的思路来构思我们的SGL程序。


作为C语言程序,main函数总是需要在最开始就了解一下的。在SGL库中,可以将main理解为

void main(){
    sgSetup();
    while(1){
        sgLoop();
    }
}


其中sgSetup函数和sgLoop函数都是需要由用户来完成的,也就是说我们负责初始化我们的SGL程序然后完成其中的循环逻辑就可以了。其根本思路就是,setup函数里设置与窗口鼠标键盘有关的属性,loop函数绘制每一帧的图案。所以,SGL程序是不会自动结束的,要结束程序的话,可以调用exit(0)这样的函数来实现。


至于loop函数里的逻辑该怎么实现,有两种方法。举个抽象点的例子,比如y = x的平方,我们要依次求出x为1,2,3…的y值,我们既可以每次直接计算x的平方,也可以根据上一次计算的y’来计算当前的y = y’ + 2*x - 1。那么这两种思路也可以应用到我们的SGL程序中。我们既可以直接算出每次loop中需要计算的变量,也可以根据上一次loop的结果进行迭代。两种方法的主要区别在于效率,需要具体情况具体分析。


下面给大家贴一个用SGL实现的简单小游戏,可以简单了解一下程序的结构,里面函数的具体用法后续文章会讨论。

#include "winsgl.h"

#define inRect(x, y, x1, y1, x2, y2) ((x>=x1&&x<=x2)&&(y>=y1&&y<=y2))
#define speedUp(e) (e+=e>0?2:-2)

void movePlayer(int x, int y);
void moveEnemy();
int hasCollision();

struct _player {
	vecTwo pos;
	bitMap *oldImage;
}player;
struct _enemy {
	vecTwo pos;
	vecTwo vel;
	bitMap *oldImage;
}enemy1, enemy2, enemy3, enemy4;
int liveTime = 0;

void sgSetup() {
	initWindow(640, 640, "Collision --SGL Sample Game", BIT_MAP);
	initMouse(SG_COORDINATE);
	player.oldImage = (bitMap*)malloc(sizeof(bitMap));
	enemy1.oldImage = (bitMap*)malloc(sizeof(bitMap));
	enemy2.oldImage = (bitMap*)malloc(sizeof(bitMap));
	enemy3.oldImage = (bitMap*)malloc(sizeof(bitMap));
	enemy4.oldImage = (bitMap*)malloc(sizeof(bitMap));
}
void sgLoop() {
	static int first = 1, cont = 0, drag = 0;
	vecTwo position;
	int press;

	if (first) {
		loadBmp(0, 0, "Source\\collision\\background.bmp");
		player.pos.x = player.pos.y = 290;
		enemy1.pos.x = 120;
		enemy1.pos.y = 100;
		enemy2.pos.x = 432;
		enemy2.pos.y = 120;
		enemy3.pos.x = 100;
		enemy3.pos.y = 432;
		enemy4.pos.x = 480;
		enemy4.pos.y = 480;
		enemy1.vel.x = enemy1.vel.y = enemy2.vel.y = enemy3.vel.x = 4;
		enemy4.vel.x = enemy4.vel.y = enemy3.vel.y = enemy2.vel.x = -4;
		getImage(player.pos.x, player.pos.y, player.pos.x + 59, player.pos.y + 59, player.oldImage);
		getImage(enemy1.pos.x, enemy1.pos.y, enemy1.pos.x + 83, enemy1.pos.y + 63, enemy1.oldImage);
		getImage(enemy2.pos.x, enemy2.pos.y, enemy2.pos.x + 135,
03-08
### SGL in IT Context In the context of Information Technology, **SGL** can refer to different concepts depending on the specific application area. One common interpretation is that it stands for *Simple Graphics Library*, which provides basic functionalities for creating and manipulating graphics. However, no direct mention of SGL appears within the provided references about LibUFO[^1] or programming with OpenGL[^2]. Instead, these sources focus more on libraries like LibUFO being utilized as toolkits for developing graphical user interfaces using technologies such as OpenGL. For instance, LibUFO serves as a C++ core library designed specifically for constructing forms or GUIs primarily functioning as an OpenGL GUI toolkit under the LGPL license since its addition on September 17, 2007. Programming involving OpenGL typically revolves around vector and matrix mathematics operations necessary for rendering three-dimensional scenes efficiently, rather than directly addressing what might be considered simple graphic libraries. If seeking detailed documentation regarding any particular implementation of SGL, exploring resources dedicated explicitly to this topic would provide more targeted insights into how such libraries operate compared to those focused on advanced visualization techniques through OpenGL-based tools. ```cpp // Example code snippet demonstrating use of vectors/matrices in OpenGL #include <GL/glut.h> void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); // Drawing simple shapes could relate loosely to 'simple' graphics glVertex2f(-0.5,-0.5); glVertex2f(0.5,0.0); glVertex2f(0.0,0.8); glEnd(); glFlush(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值