FROM:http://lazyfoo.net/tutorials/SDL/04_key_presses/index.php

Key Presses

Last Updated 6/11/19

Xing out the window is just one of the events SDL is capable of handling. Another type of input used heavily in games is the keyboard. In this tutorial we're going to make different images appear depending on which key you press.

//Key press surfaces constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
Near the top of the source code we declare an enumeration of the different surfaces we have. Enumerations are a shorthand way to do symbolic constants instead of having to do const int KEY_PRESS_SURFACE_DEFAULT = 0; const int KEY_PRESS_SURFACE_UP = 1; const int KEY_PRESS_SURFACE_DOWN = 2; and such. They default to start counting at 0 and go up by one for each enumeration you declare. This means KEY_PRESS_SURFACE_DEFAULT is 0, KEY_PRESS_SURFACE_UP is 1, KEY_PRESS_SURFACE_DOWN is 2, KEY_PRESS_SURFACE_LEFT is 3, KEY_PRESS_SURFACE_RIGHT is 4, and KEY_PRESS_SURFACE_TOTAL is 5, It's possible to give them explicit integer values, but we won't be covering that here. A quick Google search on enumerations should cover that.

One bad habit beginning programmers have is using abritary numbers instead of symbolic constants. For example they'll have 1 mean main menu, 2 mean options, etc which is fine for small programs. When you're dealing with thousands of lines of code (which video games usually do), having a line that says "if( option == 1 )" will produce much more headaches than using "if( option == MAIN_MENU )".
//Starts up SDL and creates window
bool init(); //Loads media
bool loadMedia(); //Frees media and shuts down SDL
void close(); //Loads individual image
SDL_Surface* loadSurface( std::string path ); //The window we'll be rendering to
SDL_Window* gWindow = NULL; //The surface contained by the window
SDL_Surface* gScreenSurface = NULL; //The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ]; //Current displayed image
SDL_Surface* gCurrentSurface = NULL;
Along with our usual function prototypes, we have a new

【转】Key Presses的更多相关文章

  1. Android按键事件处理流程 -- KeyEvent

    刚接触Android开发的时候,对touch.key事件的处理总是一知半解,一会是Activity里的方法,一会是各种View 中的,自己始终不清楚到底哪个在先哪个在后,总之对整个处理流程没能很好的把 ...

  2. IP-Address TextBox

    http://www.codeproject.com/Articles/4693/IP-Address-TextBox 可以下载试用效果.个人感觉功能很强大,但输入时让人不太舒服.可以参考. ntro ...

  3. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  4. SDL1.2学习

    http://wenku.baidu.com/view/c953c0878762caaedd33d4d8.html 一.安装: sudo apt-get install libsdl1.2-dev l ...

  5. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  6. Fast-paced Multiplayer

    http://www.gabrielgambetta.com/fpm1.html —————————————————————————————————————————————————————— Fast ...

  7. [翻译.每月一译.每日一段]Exploring Fonts with DirectWrite and Modern C++

    Windows with C++ Exploring Fonts with DirectWrite and Modern C++ Kenny Kerr DirectWrite is an incred ...

  8. Android Audio Play Out Channel

    1: 7嘴8舌 扬声器, 耳机, 和听筒 就是通过: audiomanager.setmode(AudioManager.MODE_IN_COMMUNICATION)audiomanager.setS ...

  9. js大小写锁判断

    <html> <head> <title>CapsLock Demo</title> <script src="http://ajax. ...

随机推荐

  1. [HarekazeCTF2019]Avatar Uploader 1 &&

    [HarekazeCTF2019]Avatar Uploader 1 这是一个文件上传的题目,但是这导体是通过满足条件来获取flag的. 他有两个函数,一个是getimagesize,还有一个是FIL ...

  2. 转载:pycharm IDE 导入自定义模块

    http://www.mamicode.com/info-detail-2241193.html

  3. Android作业 0923

    计算器小应用 package com.example.myhomework2; import androidx.appcompat.app.AppCompatActivity; import andr ...

  4. IDEA搭建多模块maven项目

    目录 新建maven项目作为多模块的parent项目 新建模块 在parent项目中配置 pom api 模块搭建restful风格api Mybatis Generator的使用(mybatis 逆 ...

  5. 嘿!Mybatis

    简介 什么是Mybatis MyBatis 是一款优秀的持久层框架 它支持自定义 SQL.存储过程以及高级映射. MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作. My ...

  6. 零基础学习Kmeans聚类算法的原理与实现过程

    内容导入: 聚类是无监督学习的典型例子,聚类也能为企业运营中也发挥者巨大的作用,比如我们可以利用聚类对目标用户进行群体分类,把目标群体划分成几个具有明显特征区别的细分群体,从而可以在运营活动中为这些细 ...

  7. C++ 双冒号开头的语法是什么

    z转载:https://blog.csdn.net/LHHopencv/article/details/78353380 命名空间限定.std::string 表示std命名空间下的 string类. ...

  8. RTKLIB的主要功能

    RTKLIB是全球导航卫星系统GNSS(global navigation satellite system)的标准&精密定位开源程序包,RTKLIB由日本东京海洋大学(Tokyo Unive ...

  9. JavaScript reduce()的使用

    语法 arr.reduce(callback(accumulator, currentValue, index, array), initialValue) 参数 callback 执行数组中每个值 ...

  10. NOIP提高组2018 D1T3 【赛道修建】

    颓了好几天,终于把这到题处理了一下. 话说,其实我考场上想出正解了,但是手残,算复杂度的时候多按了一个零,导致算出来是1亿多的复杂度,都不敢打...就把部分分都捡了一下... 题目描述: C 城将要举 ...