《译 SFML Game Development By Example 英文版》—— 《第一章》“ It's Alive! It's Alive! – Setup and First Program”

本文是《SFML Game Development By Example》的第一章,介绍了如何在SFML环境中设置和打开窗口,理解基本的SFML绘图概念。文章详细讲解了如何创建窗口、处理事件、清除屏幕、绘制形状和纹理,并通过示例展示了如何使用精灵(Sprite)进行图像绘制和弹跳效果。
摘要由CSDN通过智能技术生成

目录

Preface

Opening a window

Basics of SFML drawing

Drawing images in SFML

What is a sprite?


Preface


在这本章中,我们将讨论:

  • 在你的机器上 和 IDE 上安装SFML 环境
  • 一个普通SFML应用程序的流程
  • 打开和管理窗口
  • 渲染的基本要素

 


Opening a window


正如您可能知道的,在屏幕上绘图需要一个窗口。幸运的是,SFML允许我们轻松打开和管理我们自己的窗口! 让我们像往常一样向我们的项目添加一个名为Main.cpp的文件。这将是应用程序的入口点。一个基本应用程序的基本框架如下:
 

#include <SFML/Graphics.hpp>

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

{

}

注意,我们已经包含了SFML graphics 的头文件。这将为我们提供打开窗口和绘制窗口所需的一切,因此,不再赘述,让我们看看打开窗口的代码:

int main()
{

    sf::RenderWindow window(sf::VideoMode(640, 480),"First window!");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // Close window button clicked.
                window.close();
            }
        }
        window.clear(sf::Color::Black);
        // Draw here.
        window.display();
    }
    system("pause");
    return 0;
}

 

注意:SFML使用sf名称空间,因此我们必须在其数据类型、枚举和静态类成员前面加上“sf::”前缀。

我们在这里做的第一件事是声明并初始化RenderWindow类型的窗口实例。 在这种情况下,我们使用了它的构造函数,但是可以将它留空并稍后通过传入完全相同的参数来使用它的create方法,该方法只需要两个参数:sf::videoMode和 std::string 窗口标题。VodeMode的构造函数有两个参数: 窗口宽度和高度。还有第三个可选参数,以每像素位为单位设置颜色深度。它的默认值是32,这对于渲染我们的目的来说已经足够了,所以我们不需要为此担忧。

在创建窗口实例之后,我们输入一个while循环,该循环利用窗口方法之一来检查它是否仍然打开,即isOpen。这有效地创建了我们的游戏循环,这是我们所有代码的核心部分。

 

让我们来看看一个典型游戏的样子:

 

游戏循环的目的是检查事件和输入,更新帧之间的游戏世界,这意味着移动玩家,敌人,checking for changes,等等,最后在屏幕上绘制一切。这个过程需要每秒重复多次,直到窗口关闭。每个应用程序的时间长短不一样,有的甚至高达每秒数千次迭代。在第二章中, 我们将讨论管理和限制我们的应用程序的帧率,以及让游戏以恒定的速度运行。

 

大多数应用程序需要有一种方法来检查窗口是否已经关闭、调整大小或移动。这就是事件处理的作用。SFML提供了一个 event 类,我们可以使用它来存储事件信息。在我们游戏循环的每次迭代中,我们需要利用我们窗口对象的 pollEvent 方法来检查发生的事件,并对它们进行处理。这种情况下,我们只对鼠标点击关闭窗口按钮时发出的事件感兴趣。 我们可以检查类Event的公共成员类型是否与正确的枚举成员匹配,在这种情况下它是sf :: Event :: Closed。 如果是的话,我们可以调用window实例的close方法,我们的程序将终止。

 

注意: 事件必须在所有SFML应用程序中处理。如果没有事件循环轮询事件,窗口将变得无响应,因为它不仅向用户提供事件信息,而且还为窗口本身提供了一种处理其内部事件的方法,这是窗口对移动或调整大小作出反应的必要条件。<

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Create and develop exciting games from start to finish using SFML About This Book Familiarize yourself with the SFML library and explore additional game development techniques Craft, shape, and improve your games with SFML and common game design elements A practical guide that will teach you how to use utilize the SFML library to build your own, fully functional applications Who This Book Is For This book is intended for game development enthusiasts with at least decent knowledge of the C++ programming language and an optional background in game design. What You Will Learn Create and open a window by using SFML Utilize, manage, and apply all of the features and properties of the SFML library Employ some basic game development techniques to make your game tick Build your own code base to make your game more robust and flexible Apply common game development and programming patterns to solve design problems Handle your visual and auditory resources properly Construct a robust system for user input and interfacing Develop and provide networking capabilities to your game In Detail Simple and Fast Multimedia Library (SFML) is a simple interface comprising five modules, namely, the audio, graphics, network, system, and window modules, which help to develop cross-platform media applications. By utilizing the SFML library, you are provided with the ability to craft games quickly and easily, without going through an extensive learning curve. This effectively serves as a confidence booster, as well as a way to delve into the game development process itself, before having to worry about more advanced topics such as “rendering pipelines” or “shaders.” With just an investment of moderate C++ knowledge, this book will guide you all the way through the journey of game development. The book starts by building a clone of the classical snake game where you will learn how to open a window and render a basic sprite, write well-structured code to implement the design of the game, and use the AABB bounding box collision concept. The next game is a simple platformer with enemies, obstacles and a few different stages. Here, we will be creating states that will provide custom application flow and explore the most common yet often overlooked design patterns used in game development. Last but not the least, we will create a small RPG game where we will be using common game design patterns, multiple GUI. elements, advanced graphical features, and sounds and music features. We will also be implementing networking features that will allow other players to join and play together. By the end of the book, you will be an expert in using the SFML library to its full potential. Style and approach An elaborate take on the game development process in a way that compliments the reader's existing knowledge, this book provides plenty of examples and is kind to the uninitiated. Each chapter builds upon the knowledge gained from the previous one and offers clarifications on common issues while still remaining within the scope of its own subject and retaining clarity. Table of Contents Chapter 1: It's Alive! It's Alive! – Setup and First Program Chapter 2: Give It Some Structure – Building the Game Framework Chapter 3: Get Your Hands Dirty – What You Need to Know Chapter 4: Grab That Joystick – Input and Event Management Chapter 5: Can I Pause This? – Application States Chapter 6: Set It in Motion! – Animating and Moving around Your World Chapter 7: Rediscovering Fire – Common Game Design Elements Chapter 8: The More You Know – Common Game Programming Patterns Chapter 9: A Breath of Fresh Air – Entity Component System Continued Chapter 10: Can I Click This? – GUI Fundamentals Chapter 11: Don't Touch the Red Button! – Implementing the GUI Chapter 12: Can You Hear Me Now? – Sound and Music Chapter 13: We Have Contact! – Networking Basics Chapter 14: Come Play with Us! – Multiplayer Subtleties
SFML is a cross-platform software development library written in C++ with bindings available for many programming languages. It provides a simple interface to the various components of your PC, to ease the development of games and multimedia applications. This book will help you become an expert of SFML by using all of its features to its full potential. It begins by going over some of the foundational code necessary in order to make our RPG project run. By the end of chapter 3, we will have successfully picked up and deployed a fast and efficient particle system that makes the game look much more ‘alive’. Throughout the next couple of chapters, you will be successfully editing the game maps with ease, all thanks to the custom tools we’re going to be building. From this point on, it’s all about making the game look good. After being introduced to the use of shaders and raw OpenGL, you will be guided through implementing dynamic scene lighting, the use of normal and specular maps, and dynamic soft shadows. However, no project is complete without being optimized first. The very last chapter will wrap up our project by making it lightning fast and efficient. What You Will Learn Dive deep into creating complex and visually stunning games using SFML, as well as advanced OpenGL rendering and shading techniques Build an advanced, dynamic lighting and shadowing system to add an extra graphical kick to your games and make them feel a lot more dynamic Craft your own custom tools for editing game media, such as maps, and speed up the process of content creation Optimize your code to make it blazing fast and robust for the users, even with visually demanding scenes Get a complete grip on the best practices and industry grade game development design patterns used for AAA projects Table of Contents Chapter 1. Under the Hood - Setting up the Backend Chapter 2. Its Game Time! - Designing the Project Chapter 3. Make It Rain! - Building a Particle System Chapter 4. Have Thy Gear Ready - Building Game Tools Chapter 5. Filling the Tool Belt - a few More Gadgets Chapter 6. Adding Some Finishing Touches - Using Shaders Chapter 7. One Step Forward, One Level Down - OpenGL Basics Chapter 8. Let There Be Light - An Introduction to Advanced Lighting Chapter 9. The Speed of Dark - Lighting and Shadows Chapter 10. A Chapter You Shouldnt Skip - Final Optimizations
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值